Search code examples
iosobjective-ccloudkit

How to access saved photos to CloudKit?


I am tying and succeeded to save a photo using CloudKit, at least I think I did, because I don't know a user friendly way to check if I am right. I am asking these in the scenario that the users makes a few photos and the photos are saved using cloudKit and then he deletes the app but theoretically he should still be able to access the data via some sort of interface provided by apple, am I right?

And here is the core that I am using:

- (void) saveToiCloudImageNamed: (NSString *)imageName andTimeCreated:(NSString *)timeCreated{

    CKContainer *container = [CKContainer defaultContainer];
    CKDatabase *privateDatabase = [container publicCloudDatabase];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,   YES);
    NSString *documentsDirectory = [paths firstObject];
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:imageName];
    NSURL *fileUrl = [[NSURL alloc] initFileURLWithPath:fullPath];

    CKAsset *photoAsset = [[CKAsset alloc] initWithFileURL:fileUrl];

    CKRecord *record = [[CKRecord alloc] initWithRecordType:@"Photo"];
    record[@"asset"] = photoAsset;
    record[@"name"] = timeCreated;

    [privateDatabase saveRecord:record
              completionHandler:^(CKRecord *record, NSError *error) {
                  if (error==nil) {
                      NSLog(@"The save was successful");
                      //Do something
                  }else{
                      NSLog(@"Error saving with localizedDescription: %@", error.localizedDescription);
                      NSLog(@"CKErrorCode = %lu", (long)[error code]);

                      if ([error code]==CKErrorNetworkFailure) {
                          double retryAfterValue = [[error.userInfo valueForKey:CKErrorRetryAfterKey] doubleValue];
                          NSLog(@"Error code network unavailable retrying after %f", retryAfterValue);

                          //                                                                              NSTimer *timer = [NSTimer timerWithTimeInterval:retryAfterValue
                          //                                                                                                                       target:self
                          //                                                                                                                     selector:@selector(testOutCloudKit)
                          //                                                                                                                     userInfo:nil
                          //                                                                                                                      repeats:NO];
                          //                                                                              [timer fire];
                      }
                  }
              }];

}

Solution

  • At the moment there is no interface for accessing CloudKit data other than what you create yourself. Maybe you want to use iCloud documents instead.

    When you save to the public database (as you are doing in the sample above) then you could access the data using the CloudKit dashboard. But that is only accessible by member in your Apple developer account.