Cloudkit: how to access basic user attributes?

I am trying to create an application in which each user will have a personal avatar, nickname and so on.

I do not know how to access the user account in cloudkit.

My solution would be to get an AppleID user, request Cloudkit and change the user attribute ... But I think there should be a better way to do this. Isn't it something like NSUserDefault, but for the cloud?

0
swift nsuserdefaults cloudkit
source share
1 answer

First you need to find out what the current login in userId is. You can do this by running the fetchUserRecordIDWithCompletionHandler method in the container. Then you can get more (currently only the first and last name) by running the discoveryUserInfoWithUserRecordID command in this container.

You can expand the Users table with additional custom fields, but I would not recommend it. The Users table is a special system table with some limitations. It would be better to create a separate post type with user preferences. For ease of access, just add the CKReference to the users table.

You should also be aware that a user can log out and log in with a different iCloud account while your application is running. You can fix this by running the code below immediately after starting your application. Of course you must implement your own logic when you see NSLog

var ubiquityIdentityDidChangeNotificationToken = NSNotificationCenter.defaultCenter().addObserverForName(NSUbiquityIdentityDidChangeNotification, object: nil, queue: nil) { _ in NSLog("The user's iCloud login changed: should refresh all user data.") } 
0
source share

All Articles