Skip to content

Instantly share code, notes, and snippets.

@jazzedge
Created November 29, 2017 21:10
Show Gist options
  • Select an option

  • Save jazzedge/2116d3f38dd3eca98bfaac7b406fd8dd to your computer and use it in GitHub Desktop.

Select an option

Save jazzedge/2116d3f38dd3eca98bfaac7b406fd8dd to your computer and use it in GitHub Desktop.
Within the scope of an application’s cloud container, each user has a unique, application specific iCloud user ID and a user info record.
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