Search code examples
cocoacocoa-touchicloudcloudkit

Unique Identifier for iCloud / CloudKit User


Is there a better solution (then the possible workaround below) to get an UUID for a iCloud / CloudKit User?
So it's possible to detect if the user already did something (on a different device) with same iCloud account.

Possible workaround:

  • Create UUID and store it in the private database
    • Disadvantage: uses extra database requests

Solution

  • I found a possible solution:

    Swift 2

    CKContainer.default().fetchUserRecordID(completionHandler: { (recordID: CKRecordID?, error: NSError?) in
        print(recordID?.recordName) // UUID String for iCloud user
    })
    

    Swift 3

    CKContainer.default().fetchUserRecordID(completionHandler: { (recordID: CKRecordID?, error: Error?) in
        print(recordID?.recordName) // UUID String for iCloud user
    })
    

    This id is the same across all devices using the same iCloud Account.