Created
December 1, 2017 06:46
-
-
Save jazzedge/f7a6a48f20a5008e4b3be3fb8ad008c6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
See: http://www.techotopia.com/index.php/An_Introduction_to_CloudKit_Data_Storage_on_iOS_8 | |
Within the scope of an application’s cloud container, each user has a unique, application specific iCloud user ID and a user info record where the user ID is used as the record ID for the user’s info record. | |
The record ID of the current user’s info record can be obtained via a call to the fetchUserRecordID(completionHandler:) method of the container instance. Once the record ID has been obtained, this can be used to fetch the user’s record from the cloud database: | |
container.fetchUserRecordID(completionHandler: {recordID, | |
error in | |
if let err = error { | |
// Failed to get record ID | |
} else { | |
// Success – fetch the user’s record here | |
} | |
The record is of type CKRecordTypeUserRecord and is initially empty. Once fetched, it can be used to store data in the same way as any other CloudKit record. | |
CloudKit can also be used to perform user discovery. This allows the application to obtain an array of the users in the current user’s address book who have also used the app. In order for the user’s information to be provided, the user must have run the app and opted in to provide the information. User discovery is performed via a call to the discoverAllIdentities(completionHandler:) method of the container instance. | |
The discovered data is provided in the form of an array of CKApplicationUserInfo objects which contain the user’s iCloud ID, first name and last name. The following code fragment, for example, performs a user discovery operation and outputs to the console the first and last names of any users that meet the requirements for discoverability: | |
container.discoverAllIdentities(completionHandler: ( | |
{users, error in | |
if let err = error { | |
print("discovery failed %@", | |
err.localizedDescription) | |
} else { | |
for userInfo in user { | |
let userRecordID = userInfo.userRecordID | |
print("First Name = %@", userInfo.firstName) | |
print("Last Name = %@", userInfo.lastName) | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment