之前需求提出想要像安卓一样从手机的文件夹获取文档
安卓那种肯定做不到,只能用像QQ、微信之类的外部app传入文档。在网上看了一些资料,总结如下:
1、拷贝文档方式,如图
这是从QQ上打开的word文档,然后点击用其他应用打开,图中第二行,可以找到很多其他app,问题是如何让自己的app处于其中。
方法如下:
其实只需要在info.plist注册文件类型,就可以在其他app中找到了
需要在info.plist
文件中,添加一个新的属性Document type,
用source code方式把下面的内容粘贴进去就行,可以删除自己不需要的文件类型
-
<key>CFBundleDocumentTypes</key>
-
<array>
-
<dict>
-
<key>CFBundleTypeName</key>
-
<string>PDF</string>
-
<key>LSHandlerRank</key>
-
<string>Owner</string>
-
<key>LSItemContentTypes</key>
-
<array>
-
<string>com.adobe.pdf</string>
-
</array>
-
</dict>
-
<dict>
-
<key>CFBundleTypeName</key>
-
<string>Microsoft Word</string>
-
<key>LSHandlerRank</key>
-
<string>Alternate</string>
-
<key>LSItemContentTypes</key>
-
<array>
-
<string>com.microsoft.word.doc</string>
-
<string>com.microsoft.word.wordml</string>
-
<string>org.openxmlformats.wordprocessingml.document</string>
-
</array>
-
</dict>
-
<dict>
-
<key>CFBundleTypeName</key>
-
<string>Microsoft Excel</string>
-
<key>LSHandlerRank</key>
-
<string>Alternate</string>
-
<key>LSItemContentTypes</key>
-
<array>
-
<string>com.microsoft.excel.xls</string>
-
<string>org.openxmlformats.spreadsheetml.sheet</string>
-
</array>
-
</dict>
-
<dict>
-
<key>CFBundleTypeIconFiles</key>
-
<array/>
-
<key>CFBundleTypeName</key>
-
<string>Microsoft PowerPoint</string>
-
<key>LSHandlerRank</key>
-
<string>Alternate</string>
-
<key>LSItemContentTypes</key>
-
<array>
-
<string>com.microsoft.powerpoint.ppt</string>
-
<string>org.openxmlformats.presentationml.presentation</string>
-
<string>public.presentation</string>
-
</array>
-
</dict>
-
<dict>
-
<key>CFBundleTypeName</key>
-
<string>Text</string>
-
<key>LSHandlerRank</key>
-
<string>Alternate</string>
-
<key>LSItemContentTypes</key>
-
<array>
-
<string>public.text</string>
-
<string>public.plain-text</string>
-
<string>public.utf8-plain-text</string>
-
<string>public.utf16-external-plain-text</string>
-
<string>public.utf16-plain-text</string>
-
<string>com.apple.traditional-mac-plain-text</string>
-
<string>public.source-code</string>
-
<string>public.c-source</string>
-
<string>public.objective-c-source</string>
-
<string>public.c-plus-plus-source</string>
-
<string>public.objective-c-plus-plus-source</string>
-
<string>public.c-header</string>
-
<string>public.c-plus-plus-header</string>
-
<string>com.sun.java-source</string>
-
<string>public.script</string>
-
<string>public.shell-script</string>
-
</array>
-
</dict>
-
<dict>
-
<key>CFBundleTypeName</key>
-
<string>Rich Text</string>
-
<key>LSHandlerRank</key>
-
<string>Alternate</string>
-
<key>LSItemContentTypes</key>
-
<array>
-
<string>public.rtf</string>
-
<string>com.apple.rtfd</string>
-
<string>com.apple.flat-rtfd</string>
-
</array>
-
</dict>
-
<dict>
-
<key>CFBundleTypeName</key>
-
<string>HTML</string>
-
<key>LSHandlerRank</key>
-
<string>Alternate</string>
-
<key>LSItemContentTypes</key>
-
<array>
-
<string>public.html</string>
-
<string>public.xhtml</string>
-
</array>
-
</dict>
-
<dict>
-
<key>CFBundleTypeName</key>
-
<string>Image</string>
-
<key>LSHandlerRank</key>
-
<string>Alternate</string>
-
<key>LSItemContentTypes</key>
-
<array>
-
<string>public.image</string>
-
</array>
-
</dict>
-
</array>
然后再Appdelegate中添加以下方法
-
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options{
-
// 判断传过来的url是否为文件类型
-
if ([url.scheme isEqualToString:@"file"]) {
-
//todo 操作文档
-
}
-
return YES;
-
}
这样就可以获取并操作外部传过来的文档了。
2.存储到“文件”方式(iOS11之后可用),如图
在外部app存储文档到自己app的“文件夹”
在自己app内部可以打开如上图方式的文件夹,然后获取到文档
方法如下:
也是在info.plist中添加一组键值,Supports Document Browser设置为YES
-
<key>UISupportsDocumentBrowser</key>
-
<true/>
这样就可以在打开微信或qq等外部app中将他们的文档存储到我们app的文件夹中,再从我们的app中打开文件夹界面,选择存储好的文档,代码如下(随便写在一个控制器中的):
-
- (IBAction)presentDocumentPicker:(id)sender {
-
//类型可以根据需求自己加
-
NSArray *documentTypes = @[@"com.microsoft.word.doc",@"com.microsoft.excel.xls"];
-
UIDocumentPickerViewController *pickerVC = [[UIDocumentPickerViewController alloc]initWithDocumentTypes:documentTypes inMode:UIDocumentPickerModeOpen];
-
pickerVC.delegate = self;
-
[self presentViewController:pickerVC animated:YES completion:nil];
-
}
-
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray <NSURL *>*)urls{
-
//获取文档url,操作文档
-
}
以上就是两种导入外部文档的方式,将就用着