2013-01-31 38 views
0

我嘗試使用下面的代碼將文件上傳到iCloud上傳數據到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"} 

感謝

+0

請確保您的所有權利都是正確的,並且您已將應用程序標記爲dev設備中的iCloud。 – logancautrell

+0

是的,我檢查了它,它是正確的 –

回答

1

使用GCD,並從不同的線程調用iCloud的代碼(特別NSURL * UBIQ = ...行)。在主線程中調用它可能會在某些情況下阻止應用程序。

您構建您的iCloud路徑的方式對我來說看起來很奇怪。試試這個:

NSURL *destinationURL = [[ubiq URLByAppendingPathComponent:@"Documents"] URLByAppendingPathComponent:@"test.png"]; 
+0

我應該上傳文件在不同的線程,或者只是NSURL * ubiq = ...只有線? –

+0

我試圖這樣做,併產生以下錯誤 「操作無法完成。操作不允許」 –

+0

@ahmedelbagoury我通常做不同的線程上的所有iCloud的東西,併發布UI更新/有關您的問題的通知等,我沒有明確的答案,還有其他與iCloud工作有關的事情嗎?你可以在NSUbiquitousKeyValueStore上存儲簡單的密鑰對象嗎? –