IOS调用 系统文件APP

iOS调用文件app(file.app)选择文件和下载

1:证书文件配置

在Identifiers下创建你的iCloud Containers配置,点击“+”创建


屏幕快照 2020-04-30 下午4.04.03.png

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

打开工程中的配置如下


Xcode工程配置

工程配置icloud.jpg

4:可选配置

在info.plist中添加如下两个配置
Supports opening documents in place
Application supports iTunes file sharing
结果都为YES
设置完以后,会在文件app中有一个和你工程名相同的文件夹出现。

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];
}

遵守代理如 和选择文件


#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;
}

你可能感兴趣的:(IOS调用 系统文件APP)