iOS 讓App中Documents文件夾內(nèi)容在Files App中可見

//注意事項, info.plist 增加
Application supports iTunes file sharing: YES
Supports opening documents in place: YES

附錄: 從系統(tǒng)Files App中讀取文件

- (void)presentDocumentPicker {
    NSArray *documentTypes = @[@"public.content", @"public.text", @"public.source-code", @"public.image", @"public.audiovisual-content", @"com.adobe.pdf", @"com.apple.keynote.key", @"com.microsoft.word.doc", @"com.microsoft.excel.xls", @"com.microsoft.powerpoint.ppt"];
    
    /*
     UIDocumentPickerModeImport,//導入
     UIDocumentPickerModeOpen,//打開
     UIDocumentPickerModeExportToService,//導出
     UIDocumentPickerModeMoveToService//直接訪問
     */
    UIDocumentPickerViewController *documentPickerViewController = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:documentTypes inMode:UIDocumentPickerModeOpen];//UIDocumentPickerModeImport
    documentPickerViewController.delegate = self;
   
    [self presentViewController:documentPickerViewController animated:YES completion:nil];

}


- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls API_AVAILABLE(ios(11.0)) {
    //獲取授權(quán)
    BOOL fileUrlAuthozied = [urls.firstObject startAccessingSecurityScopedResource];
    if (fileUrlAuthozied) {
        //通過文件協(xié)調(diào)工具來得到新的文件地址,以此得到文件保護功能
        NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] init];
        NSError *error;

        [fileCoordinator coordinateReadingItemAtURL:urls.firstObject options:0 error:&error byAccessor:^(NSURL *newURL) {
            [self handleReadingItemAtURL:newURL];
//            [self dismissViewControllerAnimated:YES completion:NULL];
        }];
        
        [urls.firstObject stopAccessingSecurityScopedResource];
        
    } else {
        //授權(quán)失敗
        [self mb_showMessage:NSLocalizedString(@"打開失敗", nil)];
    }
}

- (void)handleReadingItemAtURL:(NSURL *)newURL {
    //自身App路徑判斷
    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];

    if ([newURL.absoluteString containsString:documentsPath]) {
        [self openFileWithUrl:newURL];
        return;
    }
    

    [self mb_Loading];
    
    NSString *fileName = [newURL lastPathComponent];
    NSError *error = nil;
    NSData *fileData = [NSData dataWithContentsOfURL:newURL options:NSDataReadingMappedIfSafe error:&error];
    
    if (error) {
        NSLog(@"error: %@",error);
        [self mb_showMessage:NSLocalizedString(@"打開失敗", nil)];
    } else {
        /*
         file:///private/var/mobile/Containers/Data/Application/289E3E40-52D3-4187-8FC0-CE8EBABD565E/Documents/example2.MOV
         */
        NSLog(@"coordinateReadingItemAtURL: %@",newURL);
        
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            
            NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:fileName];
            BOOL ret = [fileData writeToFile:path atomically:YES];
            NSLog(@"寫文件:%@", ret?@"成功":@"失敗");
            
            if (ret) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self mb_hideHUD];
                    
                    NSURL *playerURL = [NSURL fileURLWithPath:path];
                    [self openFileWithUrl:playerURL];
                    
                });
                
            } else {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self mb_showMessage:NSLocalizedString(@"打開失敗", nil)];
                    
                });
            }
            
            
        });
        
    }
    
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容