【问题标题】:iCloud Drive - Read & Write (NSData)iCloud Drive - 读写 (NSData)
【发布时间】:2017-08-20 14:09:02
【问题描述】:

尝试将自定义对象保存为 NSData,然后上传到 iCloud Drive

任何方向? 我已经尝试过 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");
     }];
}

编辑:

【问题讨论】:

  • 看起来你和我正在同时处理同一个问题,直到很快就会发布答案。

标签: objective-c icloud-drive


【解决方案1】:
// 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>

【讨论】:

  • 效果很好,别忘了在 xCode 上 - 启用“iCloud 功能” 祝福你 √
  • IOS 10 无法运行的原因?所有设备的 iCloud 设置都已开启
  • @OfirMalachi 我在 iOS 10 上进行的测试对我有用,请检查您是否在 Plist 中添加了必要的 iCloud 设置,并且您的配置文件添加了一个容器。
  • 我应该在 .Plist 文件中添加什么?
  • @OfirMalachi 更新了我的答案,请确保分别添加您的 CloudContainerName 和 CloudContainerID。
猜你喜欢
  • 2016-12-22
  • 1970-01-01
  • 2023-03-20
  • 2016-02-16
  • 1970-01-01
  • 2019-12-27
  • 1970-01-01
  • 2015-12-02
  • 2015-05-19
相关资源
最近更新 更多