美文网首页iOS开发常用知识点
iOS 将文件从第三方app分享到自己的app

iOS 将文件从第三方app分享到自己的app

作者: 哇哈哈有点甜 | 来源:发表于2019-11-15 15:56 被阅读0次

引言

现实的场景中,可能会有很多小伙伴需要通过微信QQ第三方app将文件(图片视频小电影文档等)分享到 自己的app。例如拉钩上传个人简历附件功能

正题

其实苹果爸爸早就考虑了这个问题,下面我们就开始集成

首先
1.配置Info.plist文件,使项目能够接收到第三方app分享过来的文件

进入项目,选中Info.plist文件,右键选择Open As Source code

InfoPlist.png
然后直接复制下面的代码。注意,不要插入到已有的元素中
   <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>

然后黏贴,再command+s。第一步搞定!

然后
2.完成了第1步后,在其他app的“用第其他应用打开”的应用列表中就会出现你app的选项了
第三方应用列表.jpeg

剩下的就是要保存到app本地,从第三方app发送过来的文件将会通过

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options {
    return YES;
}

url回调出来,下面是笔者自己写的存储逻辑,仅供参考

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
    if (self.window) {
        if (url) {
            NSFileManager *fileManager = [NSFileManager defaultManager];
            NSString *fileNameStr = [NSString stringWithFormat:@"自定义文件夹名称/%@", [url lastPathComponent]];
            NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
            NSString *filePath = [documentPath stringByAppendingPathComponent:@"文件名"];
            if (![fileManager fileExistsAtPath:filePath]) {
                [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
            }else {
                NSError *error = nil;
                NSArray *fileList = [fileManager contentsOfDirectoryAtPath:filePath error:&error];
                if (!error) {
                    //清除之前存储的文件,如果不需要可以不加
                    BOOL isDir = NO;
                    for (NSString *file in fileList) {
                        NSString *path = [filePath stringByAppendingPathComponent:file];
                        [fileManager fileExistsAtPath:path isDirectory:(&isDir)];
                        if (!isDir) {
                            NSError *removeError = nil;
                            [fileManager removeItemAtPath:path error:&removeError];
                            if (removeError) {
                                NSLog(@"清除%@失败:%@", path, removeError.description);
                            }
                        }
                        isDir = NO;
                    }
                }
            }
            //存储文件
            NSData *data = [NSData dataWithContentsOfURL:url];
            BOOL success = [data writeToFile:[NSString stringWithFormat:@"%@/%@", filePath, [url lastPathComponent]] atomically:YES];
            NSLog(@"文件%@%@存到本地文件夹内", fileNameStr, success ? @"已经":@"没有");
        }
    }
    
    return YES;
}

最后

大功告成!剩下的可以根据个人业务需要对本地文件进行操作。多谢查阅😄😄

相关文章

网友评论

    本文标题:iOS 将文件从第三方app分享到自己的app

    本文链接:https://www.haomeiwen.com/subject/dbksictx.html