Home Unable to share a CKRecord
Reply: 0

Unable to share a CKRecord

user4992124
1#
user4992124 Published in 2017-12-07 20:31:05Z

I wrote the code below to share a CKRecord:

CKRecordZone *restaurantsZone = [[CKRecordZone alloc] initWithZoneName:@"RestaurantsZone"];
CKRecordID *recordID = [[CKRecordID alloc] initWithRecordName:self.recordName zoneID:restaurantsZone.zoneID];
CKRecord *record = [[CKRecord alloc] initWithRecordType:@"Restaurant" recordID:recordID];
[record setValue:self.restaurant forKey:@"name"];

UICloudSharingController *cloudSharingController = [[UICloudSharingController alloc] initWithPreparationHandler:^(UICloudSharingController * _Nonnull controller, void (^ _Nonnull preparationCompletionHandler)(CKShare * _Nullable, CKContainer * _Nullable, NSError * _Nullable)) {
    [self shareRootRecord:record name:self.restaurant completion:preparationCompletionHandler];
}];
cloudSharingController.delegate = self;
[self presentViewController:cloudSharingController animated:YES completion:nil];

And the shareRootRecord function:

- (void)shareRootRecord:(CKRecord *)rootRecord name:(NSString *)name completion:(void (^)(CKShare * _Nullable share, CKContainer * _Nullable container, NSError * _Nullable error))completion
{
    CKShare *shareRecord = [[CKShare alloc] initWithRootRecord:rootRecord];
    shareRecord[CKShareTitleKey] = name;
    NSArray *recordsToSave = @[rootRecord, shareRecord];
    CKContainer *container = [CKContainer defaultContainer];
    CKDatabase *privateDatabase = [container sharedCloudDatabase];

    CKModifyRecordsOperation *operation = [[CKModifyRecordsOperation alloc] initWithRecordsToSave:recordsToSave recordIDsToDelete:@[]];
    [operation setPerRecordCompletionBlock:^(CKRecord * _Nullable record, NSError * _Nullable error) {
        if (error) {
            NSLog(@"%@", [error localizedDescription]);
        }
    }];
    [operation setModifyRecordsCompletionBlock:^(NSArray<CKRecord *> * _Nullable savedRecords, NSArray<CKRecordID *> * _Nullable deletedRecordIDs, NSError * _Nullable error) {
        if (error) {
            NSLog(@"%@", [error localizedDescription]);
        }
        completion(shareRecord, container, error);
    }];

    [privateDatabase addOperation:operation];
}

Now, when I run this code, the following error is thrown: Only shared zones can be accessed in the shared DB. I can't seem to be able to figure out why, though. Any ideas?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.321971 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO