Skip to content

Instantly share code, notes, and snippets.

@ccabanero
Created September 30, 2014 13:20
Show Gist options
  • Save ccabanero/17ded1cdaa68517bfb22 to your computer and use it in GitHub Desktop.
Save ccabanero/17ded1cdaa68517bfb22 to your computer and use it in GitHub Desktop.
CloudKit: How to create a new record in a Container's Public Database (Swift)
//create a new RecordType
let wellKnownID: CKRecordID = CKRecordID(recordName: "1")
let pointOfInterest: CKRecord = CKRecord(recordType: "POI", recordID: wellKnownID)
//create and set record instance properties
pointOfInterest.setObject("Sample point of interest", forKey: "title")
pointOfInterest.setObject("My favorite point of interest", forKey: "description")
pointOfInterest.setObject("123 Main Street, Endor", forKey: "address")
let location: CLLocation = CLLocation(latitude: 47.605024, longitude: -122.335274)
pointOfInterest.setObject(location, forKey: "location")
//get the PublicDatabase from the Container
let defaultContainer: CKContainer = CKContainer.defaultContainer()
let publicDatabase: CKDatabase = defaultContainer.publicCloudDatabase
//save the record to the target database
publicDatabase.saveRecord(pointOfInterest, completionHandler: { (record, error) -> Void in
if error != nil {
println("Uh oh, there was an error saving...")
println(error.localizedDescription)
}
if record != nil {
println("Saved Successflly")
println(record.objectForKey("title"))
println(record.objectForKey("description"))
println(record.objectForKey("address"))
println(record.objectForKey("location"))
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment