总体来说,还是有一点点小坑存在吧。
如果需要使用苹果的iCloud共享,那么你需要在开发者中心,将当前Bundle ID对应的iCloud权限给打开。这里有一点小坑,在选择Xcode5和Xcode6支持上,我先选择的Xcode6支持,但是不知道为何,我的Containers创建显示一直都是红色的,后来改成Xcode5支持的时候,突然变成黑色,具体的问题没有进行深入研究,有知道的朋友可以帮忙解答一下疑惑,谢谢。
OK,接下来,在我们的Xcode上需要开启iCloud权限。这个名字的格式就是iClou.[App的Bundle ID]
Xcode开启iCloud.png
然后在我们的info.plist文件中也需要进行设置相关信息。其中“Xin”就是你共享到iCloud上文件夹到名字。
iCloud在info中设置.png
OK,到了我们最喜欢的代码环节。
/// iCloud
- (void)iCloudAction {
//默认的icloud Documents容器的Indentifier
NSString * const ubiquityIndentifier = @"iCloud.com.face.magic";
NSURL *url = getDocumentUrlWithPath(nil);
NSArray *documents = @[@"4.pdf"];
for (NSString *documentName in documents)
{
BOOL t = [[NSFileManager defaultManager] copyItemAtPath:[[NSBundle mainBundle].resourcePath stringByAppendingPathComponent:documentName]
toPath:[url.path stringByAppendingPathComponent:documentName]
error:nil];
NSLog(@"isSuccess : %d",t);
}
}
//获取icloud Documents根文件夹或者子文件夹地址
NSURL* getDocumentUrlWithPath(NSString *path)
{
// NSString * const ubiquityIndentifier = @"iCloud.com.face.magic";
NSString * const ubiquityIndentifier = @"iCloud.com.face.magic";
NSURL *url = nil;
url = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:ubiquityIndentifier];
NSLog(@"------url:%@",url);
if (url && path) {
url = [[url URLByAppendingPathComponent:@"Documents"] URLByAppendingPathComponent:path];
return url;
}else if(!path) {
// 如果不存在文件夹,则直接创建一个文件夹
if (![[NSFileManager defaultManager] fileExistsAtPath:[url URLByAppendingPathComponent:@"Documents"].path]) {
[[NSFileManager defaultManager] createDirectoryAtURL:[url URLByAppendingPathComponent:@"Documents"] withIntermediateDirectories:YES attributes:nil error:nil];
}
return [url URLByAppendingPathComponent:@"Documents"];
}else {
NSLog(@"Please check out that iCloud had been signed in");
return url;
}
return url;
}
这里有一个很重要的环节就是,如果没有Documents目录的话,创建一个,之前没有添加这个,文件一直说存在,但是就是没有展示出来,创建Documents后,文件已经能够正常展示了。
如果想要查看的话,可以通过iPhone手机的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"];
UIDocumentPickerViewController *documentPickerViewController = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:documentTypes
inMode:UIDocumentPickerModeOpen];
// documentPickerViewController.delegate = self;
[self presentViewController:documentPickerViewController animated:YES completion:nil];
}
这里我在放上之前写的有坑的代码,部分能够成功,不过有时会存在bug问题。
/// iCloud 导入2。- BUG:文件可能失败
- (void)iCloudImportAction {
NSLog(@"uploadDoc");
//文档名字
// NSString *fileName =@"1.txt";
NSString *fileName =@"4.pdf";
NSURL *url = [self getUbiquityContainerUrl:fileName];
// NSURL *url = getDocumentUrlWithPath(fileName);
MyDocument *doc = [[MyDocument alloc] initWithFileURL:url];
// UIDocument *doc = [[UIDocument alloc] initWithFileURL:url];
// 文档内容
// NSString*str = @"测试文本数据";
// doc.myData = [str dataUsingEncoding:NSUTF8StringEncoding];
doc.myData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"4.pdf" ofType:nil]];
[doc saveToURL:url forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
if (success) {
NSLog(@"创建成功");
}
else{
NSLog(@"创建失败");
}
}];
}
//获取url
-(NSURL*)getUbiquityContainerUrl:(NSString*)fileName{
NSURL *myUrl = nil;
if (!myUrl) {
myUrl = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:UbiquityContainerIdentifier];
if (!myUrl) {
NSLog(@"未开启iCloud功能");
return nil;
}
}
NSURL *url = [myUrl URLByAppendingPathComponent:@"Documents"];
url = [url URLByAppendingPathComponent:fileName];
return url;
}
/// iCloud 导入3 - BUG:没有移动权限
- (void)btnStoreTapped {
// Let's get the root directory for storing the file on iCloud Drive
[self rootDirectoryForICloud:^(NSURL *ubiquityURL) {
NSLog(@"1. ubiquityURL = %@", ubiquityURL);
if (ubiquityURL) {
// We also need the 'local' URL to the file we want to store
NSURL *localURL = [self localPathForResource:@"4" ofType:@"pdf"];
NSLog(@"2. localURL = %@", localURL);
// Now, append the local filename to the ubiquityURL
ubiquityURL = [ubiquityURL URLByAppendingPathComponent:localURL.lastPathComponent];
NSLog(@"3. ubiquityURL = %@", ubiquityURL);
// And finish up the 'store' action
NSError *error;
if (![[NSFileManager defaultManager] setUbiquitous:YES itemAtURL:localURL destinationURL:ubiquityURL error:&error]) {
NSLog(@"Error occurred: %@", error);
}
}
else {
NSLog(@"Could not retrieve a ubiquityURL");
}
}];
}
- (void)rootDirectoryForICloud:(void (^)(NSURL *))completionHandler {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *rootDirectory = [[[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil] URLByAppendingPathComponent:@"Documents"];
if (rootDirectory) {
if (![[NSFileManager defaultManager] fileExistsAtPath:rootDirectory.path isDirectory:nil]) {
NSLog(@"Create directory");
[[NSFileManager defaultManager] createDirectoryAtURL:rootDirectory withIntermediateDirectories:YES attributes:nil error:nil];
}
}
dispatch_async(dispatch_get_main_queue(), ^{
completionHandler(rootDirectory);
});
});
}
- (NSURL *)localPathForResource:(NSString *)resource ofType:(NSString *)type {
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:resource ofType:type];
return [NSURL fileURLWithPath:resourcePath];
}
谨慎查看,避免踩坑。
网友评论