iOS調(diào)用文件app(file.app)選擇文件和下載
1:證書文件配置
在Identifiers下創(chuàng)建你的iCloud Containers配置啃勉,點(diǎn)擊“+”創(chuàng)建
2:在Identifiers下選擇你要添加icloud的boundid把icloud配置勾選上既可
打開工程中的配置如下
工程配置icloud.jpg
4:可選配置
在info.plist中添加如下兩個(gè)配置
Supports opening documents in place
Application supports iTunes file sharing
結(jié)果都為YES
設(shè)置完以后招刨,會(huì)在文件app中有一個(gè)和你工程名相同的文件夾出現(xiàn)礼饱。
5:打開文件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:@"寫入沙河" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
[alert show];
//寫入沙盒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;
}