ios調(diào)用第三方程序打開(kāi)文件,以及第三方調(diào)用自己的APP打開(kāi)文件

1.自己的APP調(diào)用第三方打開(kāi)文件
主要是使用 UIDocumentInteractionController 類(lèi) 并實(shí)現(xiàn)UIDocumentInteractionControllerDelegate 的代理方法。
<pre>
@interface HNDownFileViewController ()<UIDocumentInteractionControllerDelegate>

@property (nonatomic, strong) UIDocumentInteractionController *documentInteractionController;

@end
</pre>

<pre>
-(void)viewDidLoad {
[super viewDidLoad];
//url 為需要調(diào)用第三方打開(kāi)的文件地址
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)試,例子中打開(kāi)的是 doc文件,如果手機(jī)上裝了WPS或者office套件,就能調(diào)用這些應(yīng)用打開(kāi)。

2.第三方APP調(diào)用自己的APP,打開(kāi)文件在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ù),如果有以上類(lèi)型的文件,第三方應(yīng)用可以調(diào)用我們的APP進(jìn)行操作。
在第三方調(diào)用我們的APP后,會(huì)調(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)用時(shí)文件的沙盒地址,
@"Documents/localFile" 表示本地文件夾目錄
sourceApplication 是調(diào)用我們APP的第三方應(yīng)用是誰(shuí)
我們把url傳到我們需要用的界面
可以使用路徑查看保存到本地的文件

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容