0

我正在尝试使用以下代码将文档上传到 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"}

谢谢

4

1 回答 1

1

使用 GCD 并从不同的线程调用 iCloud 代码(特别是 NSURL *ubiq =... 行)。在某些情况下,在主线程上调用它可能会阻塞应用程序。

你构建 iCloud 路径的方式在我看来很奇怪。尝试这个:

NSURL *destinationURL = [[ubiq URLByAppendingPathComponent:@"Documents"] URLByAppendingPathComponent:@"test.png"];
于 2013-01-31T15:15:58.607 回答