iOS調(diào)用文件app(file.app)選擇文件和下載

第三方應(yīng)用調(diào)用系統(tǒng)文件app,并下載文件。

配置如下

1:證書(shū)文件配置
在Identifiers下創(chuàng)建你的iCloud Containers配置,點(diǎn)擊“+”創(chuàng)建

2:在Identifiers下選擇你要添加icloud的boundid把icloud配置勾選上既可

3:打開(kāi)工程中的配置如下

image

4:可選配置
在info.plist中添加如下兩個(gè)配置
Supports opening documents in place
Application supports iTunes file sharing
結(jié)果都為YES
設(shè)置完以后,會(huì)在文件app中有一個(gè)和你工程名相同的文件夾出現(xiàn)。

5:打開(kāi)文件app

- (void)presentDocumentCloud {
    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"];

    UIDocumentPickerViewController *documentPickerViewController = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:documentTypes inMode:UIDocumentPickerModeOpen];
    documentPickerViewController.delegate = self;
    [self presentViewController:documentPickerViewController animated:YES completion:nil];
}

遵守代理如下

<UIDocumentPickerDelegate, UIDocumentInteractionControllerDelegate>

選擇文件事件

#pragma mark - UIDocumentPickerDelegate
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url {

    NSArray *array = [[url absoluteString] componentsSeparatedByString:@"/"];
    NSString *fileName = [array lastObject];
    fileName = [fileName stringByRemovingPercentEncoding];
    NSLog(@"--->>>>%@",fileName);
    if ([iCloudManager iCloudEnable]) {
        [iCloudManager downloadWithDocumentURL:url callBack:^(id obj) {
            NSData *data = obj;
            UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"icloud" message:@"寫(xiě)入沙河" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
            [alert show];
            //寫(xiě)入沙盒Documents
            NSString *path = [NSHomeDirectory() stringByAppendingString:[NSString stringWithFormat:@"/Documents/%@",fileName]];
            [data writeToFile:path atomically:YES];
        }];
    }
}

6:判斷icloud是否可用

+ (BOOL)iCloudEnable {
    NSFileManager *manager = [NSFileManager defaultManager];
    NSURL *url = [manager URLForUbiquityContainerIdentifier:nil];
    if (url != nil) {
        return YES;
    }
    NSLog(@"iCloud 不可用");
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"icloud" message:@"iCloud 不可用" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
    [alert show];
    return NO;
}

下載工程

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容