Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current user ID in CloudKit?

Tags:

ios

cloudkit

I'd like to save at most one record (i.e. a rating) per user in a table of the public database. For this I need to save the current user ID or device ID. But how can I get it?

like image 210
Jan F. Avatar asked Mar 28 '15 09:03

Jan F.


People also ask

Does Apple use CloudKit?

Overview. The CloudKit Database app is a web-based tool for developers to manage their iCloud containers. You can sign in to the CloudKit Database app through the Apple Developer web portal or through the CloudKit Console at https://icloud.developer.apple.com/.

What is Apple CloudKit?

CloudKit is a framework that lets app developers store key-value data, structured data, and assets in iCloud. Access to CloudKit is controlled using app entitlements. CloudKit supports both public and private databases.


3 Answers

Get iCloud ID / CloudKit Record ID with Swift 2

Here is a snippet which I am always using to fetch the iPhone user’s iCloud ID (Apple calls it CloudKit Record ID) for an iOS app.

The only thing you need to do in Xcode is to activate the "CloudKit" checkbox in your project’s iCloud capabilities. You do not need to actively use CloudKit at all - it is just the core of all iCloud activities since iOS 8.

It is important to know that Apple never directly exposes the real iCloud ID but always just returns a secured hash of the iCloud ID and your app ID. But that should not worry you because that string is still unique for each user of your app across all his devices and can be used as login replacement.

My function, is async and returns an optional CKRecordID object. The most interesting property of that CKRecordID object is recordName.

CKRecordID.recordName is a 33 character string where the first character is always an underscore followed by 32 unique characters (== your user iCloud ID encoded for your app). It looks similar to: "_cd2f38d1db30d2fe80df12c89f463a9e"

import CloudKit

/// async gets iCloud record name of logged-in user
func iCloudUserIDAsync(complete: (instance: CKRecordID?, error: NSError?) -> ()) {
    let container = CKContainer.defaultContainer()
    container.fetchUserRecordIDWithCompletionHandler() {
        recordID, error in
        if error != nil {
            print(error!.localizedDescription)
            complete(instance: nil, error: error)
        } else {
            print("fetched ID \(recordID?.recordName)")
            complete(instance: recordID, error: nil)
        }
    }
}


// call the function above in the following way:
// (userID is the string you are intersted in!)

iCloudUserIDAsync() {
    recordID, error in
    if let userID = recordID?.recordName {
        print("received iCloudID \(userID)")
    } else {
        print("Fetched iCloudID was nil")
    }
}
like image 158
Sebastian Avatar answered Oct 16 '22 18:10

Sebastian


You'll want to call -[CKContainer fetchUserRecordIDWithCompletionHandler:] to fetch the current user record ID:

like image 29
farktronix Avatar answered Oct 16 '22 19:10

farktronix


Here is the code snippet for Swift 3

import CloudKit

/// async gets iCloud record ID object of logged-in iCloud user
func iCloudUserIDAsync(complete: @escaping (_ instance: CKRecordID?, _ error: NSError?) -> ()) {
    let container = CKContainer.default()
    container.fetchUserRecordID() {
        recordID, error in
        if error != nil {
            print(error!.localizedDescription)
            complete(nil, error as NSError?)
        } else {
            print("fetched ID \(recordID?.recordName)")
            complete(recordID, nil)
        }
    }
}

// call the function above in the following way:
// (userID is the string you are interested in!)
iCloudUserIDAsync { (recordID: CKRecordID?, error: NSError?) in
    if let userID = recordID?.recordName {
        print("received iCloudID \(userID)")
    } else {
        print("Fetched iCloudID was nil")
    }
}
like image 8
Yi-Hsiu Lee Avatar answered Oct 16 '22 18:10

Yi-Hsiu Lee