Skip to content

Instantly share code, notes, and snippets.

@ccabanero
Last active August 29, 2015 14:07
Show Gist options
  • Save ccabanero/a4e3875439df4b97948f to your computer and use it in GitHub Desktop.
Save ccabanero/a4e3875439df4b97948f to your computer and use it in GitHub Desktop.
How to create a new record in a Container's Public Database (Objective-C)
//create a new RecordType
CKRecordID *wellKnownID = [[CKRecordID alloc] initWithRecordName:@"1"];
CKRecord *poi = [[CKRecord alloc] initWithRecordType:@"POI" recordID:wellKnownID];
//create and set record instance properties
poi[@"title"] = @"Sample point of interest";
poi[@"description"] = @"My favorite point of interest";
poi[@"address"] = @"123 Main Street, Endor";
poi[@"location"] = [[CLLocation alloc] initWithLatitude:47.605024 longitude:-122.335274];
//get the PublicDatabase from the Container for this app
CKDatabase *publicDatabase = [[CKContainer defaultContainer] publicCloudDatabase];
//save the record to the target database
[publicDatabase saveRecord:poi completionHandler:^(CKRecord *record, NSError *error) {
//handle save error
if(error) {
NSLog(@"Uh oh, there was an error saving ... %@", error);
//handle successful save
} else {
NSLog(@"Saved successfully");
NSLog(@"Title: %@", record[@"title"]);
NSLog(@"Description: %@", record[@"description"]);
NSLog(@"Address: %@", record[@"address"]);
NSLog(@"Location: %@", record[@"location"]);
}
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment