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

1.自己的APP调用第三方打开文件

主要是使用  UIDocumentInteractionController  类   并实现 UIDocumentInteractionControllerDelegate 的代理方法

@interface HNDownFileViewController ()<UIDocumentInteractionControllerDelegate>

@property (nonatomic, strong) UIDocumentInteractionController *documentInteractionController;

@end
- (void)viewDidLoad {
    [super viewDidLoad];
    //url 为需要调用第三方打开的文件地址
    NSURL *url = [NSURL fileURLWithPath:_dict[@"path"]];
        _documentInteractionController = [UIDocumentInteractionController
                                              interactionControllerWithURL:url];
        [_documentInteractionController setDelegate:self];
        
        [_documentInteractionController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
}

需要在真机上调试,例子中打开的是  doc文件,如果手机上装了WPS或者office套件,就能调用这些应用打开。

 

2.第三方APP调用自己的APP,打开文件

在info.plist中添加如下代码

<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>




- (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:@"文件已存到本地文件夹内" delay:2.0f];
        }
    }
    return YES;
}

url 就是第三方应用调用时文件的沙盒地址,

@"Documents/localFile" 表示本地文件夹目录

sourceApplication 是调用我们APP的第三方应用是谁

我们把url传到我们需要用的界面

 可以使用路径查看保存到本地的文件



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值