第三方调用自己的App打开文件

第三方调用自己的App打开文件

在程序中有这样的需求,例如QQ中有个pdf文件,然后可以让自己的app打开这个pdf文件,弹出的选择框如下:
01
先要在Target->Info->Document Types下注册app支持的类型,可参考Registering the File Types Your App Supports

即在Info.plist包含CFBundleDocumentTypesCFBundleDocumentTypes是一个字典的集合,可以包含下面的key:

  • CFBundleTypeName specifies the name of the document type.
    指定文档类型名称
  • CFBundleTypeIconFiles is an array of filenames for the image resources to use as the document’s icon.
    是一个数组,包含多个图片文件名,用于作为该文档的图标
  • LSItemContentTypes contains an array of strings with the UTI types that represent the supported file types in this group.
    是一个数组,包含多个 UTI 类型的字符串。UTI 类型是本文档类型(组)所包含的文件类型
  • LSHandlerRank describes whether this application owns the document type or is merely able to open it. (For a list of values for LSHandlerRank, see CFBundleDocumentTypes.)
    表示应用程序是“拥有”还是仅仅是“打开”这种类型而已

如进行如下的设置:

02
Types的值可以参考System-Declared Uniform Type Identifiers

之后,Info.plist会包含如下的内容:

03
设置之后,还需要在AppDelegate实现如下的方法:

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options

获取到传递过来的url,我自己测试了下,打开的文件都被复制到了沙盒的Document/Inbox目录下

如果要预览文件,可能还需要将url中的file://替换为空字符串

参考文档:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值