2017-08-20 69 views
1

試圖保存自定義對象作爲NSData的然後上傳到iCloud 驅動icloud的驅動器 - 讀寫(NSData的)

任何方向? 我已經嘗試了Key-Value方法,但限制爲1MB:/

這是正確的方法嗎? 如果是,我該如何從iCloud路徑讀取?

+(void)iCloudWrite:(NSData*)data 
{ 

    NSURL* ubiq = [[NSFileManager defaultManager]URLForUbiquityContainerIdentifier:nil]; 
    NSURL *ubiquitousPackage = [[ubiq URLByAppendingPathComponent:@"Documents"] URLByAppendingPathComponent:@"iCloudHistory.zip"]; 

    MyDocument *mydoc = [[MyDocument alloc] initWithFileURL:ubiquitousPackage]; 
    mydoc.dataContent = data; 

    [mydoc saveToURL:[mydoc fileURL] forSaveOperation:UIDocumentSaveForOverwriting completionHandler:^(BOOL success) 
    { 
     if (success) 
     { 
      NSLog(@"Synced with icloud"); 
     } 
     else 
      NSLog(@"Syncing FAILED with icloud"); 
    }]; 
} 

編輯:

enter image description here

+1

它看起來像你和我在同一問題工作在相同的情況下,將很快發佈答案。 –

回答

0
// 1. Any file can be uploaded to iCloud container of any size (yes you should be having that much of space in iCloud) lets take an example SampleData.zip 

// 2. This method will upload or sync SampleData.zip file in iCloud container, iCloud actually checks the metadata of your file before it uploads it into your iCloud container (so for first time it will upload the file and from next time it will only upload the changes) 

-(void) iCloudSyncing:(id)sender 
{ 
    //Doc dir 
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"SampleData.zip"]; 
    NSURL *u = [[NSURL alloc] initFileURLWithPath:filePath]; 
    NSData *data = [[NSData alloc] initWithContentsOfURL:u]; 

    //Get iCloud container URL 
    NSURL *ubiq = [[NSFileManager defaultManager]URLForUbiquityContainerIdentifier:nil];// in place of nil you can add your container name 
    //Create Document dir in iCloud container and upload/sync SampleData.zip 
    NSURL *ubiquitousPackage = [[ubiq URLByAppendingPathComponent:@"Documents"]URLByAppendingPathComponent:@"SampleData.zip"]; 
    Mydoc = [[MyDocument alloc] initWithFileURL:ubiquitousPackage]; 
    Mydoc.zipDataContent = data; 

    [Mydoc saveToURL:[Mydoc fileURL] forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) 
    { 
     if (success) 
     { 
      NSLog(@"SampleData.zip: Synced with icloud"); 
     } 
     else 
      NSLog(@"SampleData.zip: Syncing FAILED with icloud"); 

    }]; 
} 



    // 3 Download data from the iCloud Container 

- (IBAction)GetData:(id)sender { 

    //--------------------------Get data back from iCloud -----------------------------// 
    id token = [[NSFileManager defaultManager] ubiquityIdentityToken]; 
    if (token == nil) 
    { 
     NSLog(@"ICloud Is not LogIn"); 
    } 
    else 
    { 
     NSLog(@"ICloud Is LogIn"); 

     NSError *error = nil; 
     NSURL *ubiq = [[NSFileManager defaultManager]URLForUbiquityContainerIdentifier:nil];// in place of nil you can add your container name 
     NSURL *ubiquitousPackage = [[ubiq URLByAppendingPathComponent:@"Documents"]URLByAppendingPathComponent:@"SampleData.zip"]; 
     BOOL isFileDounloaded = [[NSFileManager defaultManager]startDownloadingUbiquitousItemAtURL:ubiquitousPackage error:&error]; 
     if (isFileDounloaded) { 
      NSLog(@"%d",isFileDounloaded); 
      NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
      //changing the file name as SampleData.zip is already present in doc directory which we have used for upload 
      NSString* fileName = [NSString stringWithFormat:@"RecSampleData.zip"]; 
      NSString* fileAtPath = [documentsDirectory stringByAppendingPathComponent:fileName]; 
      NSData *dataFile = [NSData dataWithContentsOfURL:ubiquitousPackage]; 
      BOOL fileStatus = [dataFile writeToFile:fileAtPath atomically:NO]; 
      if (fileStatus) { 
       NSLog(@"success"); 
      } 
     } 
     else{ 
      NSLog(@"%d",isFileDounloaded); 
     } 
    } 
} 

//4 voila its done :) 

不要忘記以下參數添加到您的plist

<key>NSUbiquitousContainers</key> 
    <dict> 
     <key>YourCloudContainerID</key> 
     <dict> 
      <key>NSUbiquitousContainerIsDocumentScopePublic</key> 
      <true/> 
      <key>NSUbiquitousContainerName</key> 
      <string>YourCloudContainerName</string> 
      <key>NSUbiquitousContainerSupportedFolderLevels</key> 
      <string>Any</string> 
     </dict> 
    </dict> 
+1

很好用, 不要忘了關於xCode - 要啓用「iCloud功能」** 保佑你√ –

+0

IOS 10不工作的原因? 所有設備iCloud設置爲ON –

+1

@OfirMalachi我在iOS 10上測試過,請檢查您是否在Plist中添加了必要的iCloud設置,並且您的配置文件中添加了容器。 –

相關問題