您的位置:首页 > 移动开发

iOS APP中第三方APP调用自己的APP,打开文件

2017-09-12 17:43 627 查看
根据需求需要在项目中要打开word、pdf、excel等文件,在info.plist文件中添加

<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeName</key>
<string></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>


其中,CFBundleTypeExtensions是文件类型,比如:pdf,doc,xls等,不可乱填,
CFBundleTypeIconFiles 是显示在活动列项中的图标,一般用的是app的icon,
Document Content Type UTIs 指定官方指定的文件类型,UTIs即Uniform Type Identifiers。具体可找

System-Declared Uniform Type Identifiers

第三方应用打开文件会调用下面的代理方法

/**
打开文件调用的代理方发

@param application 自己的app
@param url 第三方应用调用时文件的沙盒地址
@param sourceApplication 调用我们APP的第三方应用是谁
@param annotation
@return 返回值是YES
*/
- (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];
NSLog(@"文件已存到本地文件夹内");
}
}
return YES;
}


ios调用第三方程序打开文件,以及第三方调用自己的APP打开文件

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐