我正在尝试使用以下代码将文档上传到 iCloud
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"test.png"];
NSURL* sourceURL = [[NSURL alloc]initWithString:path];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:[sourceURL path]]) {
NSLog(@"File found!");
}
else{
NSLog(@"File not found!");
}
NSURL *ubiq = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
if (ubiq) {
NSLog(@"iCloud access at %@", ubiq);
NSError* error = nil;
NSURL *destinationURL = [ubiq URLByAppendingPathComponent:@"Documents/test.png"];
[[NSFileManager defaultManager] setUbiquitous:YES
itemAtURL:sourceURL
destinationURL:destinationURL
error:&error];
if(error != nil){
NSLog(@"Error while uploading the file: %@", error);
}
else{
NSLog(@"File ulpoaded successfully");
}
else {
NSLog(@"No iCloud access");
}
我尝试上传的文件存在(打印了“找到的文件”),但将其上传到 iCloud 会产生以下错误
Error while uploading the file: Error Domain=NSCocoaErrorDomain Code=513 "The operation couldn’t be completed. (Cocoa error 513.)" UserInfo=0x1dd6bda0 {NSURL=/var/mobile/Applications/9F496F51-9DCA-4379-A62E-FFF7D7AB7385/VerticalSwipeArticles.app/test.png, NSUnderlyingError=0x1dd6c120 "The operation couldn’t be completed. Operation not permitted"}
谢谢