1.自己的APP調(diào)用第三方打開文件
主要是使用 UIDocumentInteractionController 類 并實現(xiàn)UIDocumentInteractionControllerDelegate 的代理方法。
<pre>
@interface HNDownFileViewController ()<UIDocumentInteractionControllerDelegate>
@property (nonatomic, strong) UIDocumentInteractionController *documentInteractionController;
@end
</pre>
<pre>
-(void)viewDidLoad {
[super viewDidLoad];
//url 為需要調(diào)用第三方打開的文件地址
NSURL *url = [NSURL fileURLWithPath:_dict[@"path"]];
_documentInteractionController = [UIDocumentInteractionController
interactionControllerWithURL:url];
[_documentInteractionController setDelegate:self];
[_documentInteractionController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
}
</pre>
需要在真機(jī)上調(diào)試掂榔,例子中打開的是 doc文件,如果手機(jī)上裝了WPS或者office套件撮胧,就能調(diào)用這些應(yīng)用打開寨昙。
2.第三方APP調(diào)用自己的APP讥巡,打開文件在info.plist中添加如下代碼
<pre>
<array>
<dict>
<key>CFBundleTypeName</key>
<string>com.myapp.common-data</string>
<key>LSItemContentTypes</key>
<array>
<string>com.microsoft.powerpoint.ppt</string>
<string>public.item</string>
<string>com.microsoft.word.doc</string>
<string>com.adobe.pdf</string>
<string>com.microsoft.excel.xls</string>
<string>public.image</string>
<string>public.content</string>
<string>public.composite-content</string>
<string>public.archive</string>
<string>public.audio</string>
<string>public.movie</string>
<string>public.text</string>
<string>public.data</string>
</array>
</dict>
</array>
</pre>
這在系統(tǒng)中添加了參數(shù),如果有以上類型的文件舔哪,第三方應(yīng)用可以調(diào)用我們的APP進(jìn)行操作欢顷。
在第三方調(diào)用我們的APP后,會調(diào)用如下方法
<pre>
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation
{
if (self.window) {
if (url) {
NSString *fileNameStr = [url lastPathComponent];
NSString *Doc = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/localFile"] stringByAppendingPathComponent:fileNameStr];
NSData *data = [NSData dataWithContentsOfURL:url];
[data writeToFile:Doc atomically:YES];
[XCHUDTool showOKHud:@"文件已存到本地文件夾內(nèi)" delay:2.0f];
}
}
return YES;
}
</pre>
url 就是第三方應(yīng)用調(diào)用時文件的沙盒地址捉蚤,
@"Documents/localFile" 表示本地文件夾目錄
sourceApplication 是調(diào)用我們APP的第三方應(yīng)用是誰
我們把url傳到我們需要用的界面
可以使用路徑查看保存到本地的文件