iOS 將文件從第三方app分享到自己的app

引言

現(xiàn)實(shí)的場(chǎng)景中,可能會(huì)有很多小伙伴需要通過(guò)微信、QQ第三方app將文件(圖片、視頻、小電影、文檔等)分享到 自己的app。例如拉鉤上傳個(gè)人簡(jiǎn)歷附件功能

正題

其實(shí)蘋(píng)果爸爸早就考慮了這個(gè)問(wèn)題,下面我們就開(kāi)始集成

首先
1.配置Info.plist文件,使項(xiàng)目能夠接收到第三方app分享過(guò)來(lái)的文件

進(jìn)入項(xiàng)目,選中Info.plist文件,右鍵選擇Open As Source code

InfoPlist.png

然后直接復(fù)制下面的代碼。注意,不要插入到已有的元素中

   <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的“用第其他應(yīng)用打開(kāi)”的應(yīng)用列表中就會(huì)出現(xiàn)你app的選項(xiàng)了
第三方應(yīng)用列表.jpeg

剩下的就是要保存到app本地,從第三方app發(fā)送過(guò)來(lái)的文件將會(huì)通過(guò)

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

url回調(diào)出來(lái),下面是筆者自己寫(xiě)的存儲(chǔ)邏輯,僅供參考

- (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:@"自定義文件夾名稱(chēng)/%@", [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) {
                    //清除之前存儲(chǔ)的文件,如果不需要可以不加
                    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;
                    }
                }
            }
            //存儲(chǔ)文件
            NSData *data = [NSData dataWithContentsOfURL:url];
            BOOL success = [data writeToFile:[NSString stringWithFormat:@"%@/%@", filePath, [url lastPathComponent]] atomically:YES];
            NSLog(@"文件%@%@存到本地文件夾內(nèi)", fileNameStr, success ? @"已經(jīng)":@"沒(méi)有");
        }
    }
    
    return YES;
}

最后

大功告成!剩下的可以根據(jù)個(gè)人業(yè)務(wù)需要對(duì)本地文件進(jìn)行操作。多謝查閱????

?著作權(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)容