由于iOS的沙盒机制,并没有办法自由的访问其它app的文件。
但是,有的时候确实需要获取到其它应用里面的文件。
例如,WPS中的word文件,希望也能在自身app中获取到。
目前,为了达到这种效果。通过相关资料搜索,或许可以通过以下几个方案实现
1. UIDocumentInteractionController
其实这个是一个文件预览的类,同时也可以将文件分享出去,一般在可以进行预览文件的app中都有实现这些功能。所以或许可以通过这种方式,得到其他app中的文件。
关于这个预览,之前有写一篇文章,算是比较详细吧 ->地址
直接上例子:这里通过两个app进行处理,一个app进行分享操作,一个app进行获取操作,先上两张图。
[图片上传中...(接收文件 保存到沙盒中的路径.png-8e7a25-1557906915422-0)]
接收文件 保存到沙盒中的路径.png
//文件共享相关实现
#import "PYHDocumentViewController.h"
@interface PYHDocumentViewController ()<UIDocumentInteractionControllerDelegate>
//这里的strong 强引用不可少
@property (nonatomic, strong) UIDocumentInteractionController *documentIntController;
@end
@implementation PYHDocumentViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blueColor];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//本地文件路径 文件存在项目中 看下面路径
NSString *pathWord = [[NSBundle mainBundle].bundlePath stringByAppendingPathComponent:@"鉴权.docx"];
//创建对象 嵌入文件路径 设置代理
self.documentIntController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:pathWord]];
self.documentIntController.delegate = self;
//直接预览 然后在预览中选择是否使用其他软件打开
[self.documentIntController presentPreviewAnimated:YES];
}
//UIDocumentInteractionControllerDelegate
//返回的控制器 指明预览将在那个控制器上进行 不进行设置 无法进行预览
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller {
return self;
}
@end
info.Plist相关介绍.png
/*
文件接收 相关处理
这里主要是权限的问题,需要配置info.plist相关参数。
可以打开info.plist(Source Code 以这种形式打开) 然后复制下面的进去。涵盖了基本的文件类型,从而其它app可以分享这些类型的文件到本app
<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>Web Archive</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>LSItemContentTypes</key>
<array>
<string>com.apple.webarchive</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>
<dict>
<key>CFBundleTypeName</key>
<string>iWork Pages</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>LSItemContentTypes</key>
<array>
<string>com.apple.page.pages</string>
<string>com.apple.iwork.pages.pages</string>
<string>com.apple.iwork.pages.template</string>
</array>
</dict>
<dict>
<key>CFBundleTypeName</key>
<string>iWork Numbers</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>LSItemContentTypes</key>
<array>
<string>com.apple.numbers.numbers</string>
<string>com.apple.iwork.numbers.numbers</string>
<string>com.apple.iwork.numbers.template</string>
</array>
</dict>
<dict>
<key>CFBundleTypeName</key>
<string>iWork Keynote</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>LSItemContentTypes</key>
<array>
<string>com.apple.keynote.key</string>
<string>com.apple.iwork.keynote.key</string>
<string>com.apple.iwork.keynote.kth</string>
</array>
</dict>
<dict>
<key>CFBundleTypeName</key>
<string>Audio</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>LSItemContentTypes</key>
<array>
<string>public.audio</string>
</array>
</dict>
<dict>
<key>CFBundleTypeName</key>
<string>Movie</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>LSItemContentTypes</key>
<array>
<string>public.movie</string>
</array>
</dict>
<dict>
<key>CFBundleTypeName</key>
<string>Archive</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>LSItemContentTypes</key>
<array>
<string>public.archive</string>
</array>
</dict>
</array>
ps:
1.这样基本就可以了,但是如果你想进行多余的处理操作。
这里有一个回调方法,当其它app分享文件到本app时,会调用这个方法。可以在此方法里面做一些你想做的事情,在Appdelegate.m
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options{}
2.当其它app分享文件到本app,保存到沙盒
默认路径:Documents/Inbox/...
*/
2. Files----->(iOS 11)
用到的代码比较少
1.权限的问题:是否需要将自己的app添加进Files
2.如何实现在自己app打开Files
==>地址
3.iCloud:这个其实并不能实现多应用的文件共享,但是可以实现多设备的文件共享。
ps:如果仅仅是同一开发者,进行某些设置还是可以实现多应用的。但是感觉这种运用的可能性不太大。
==>总共有四篇:首篇:地址