问题描述
我正在尝试制作一个每个用户都有个人头像,昵称等的应用程序。
I am trying to make an app in which every user would have a personal avatar, a nickname and so on.
我不知道如何访问cloudkit上的用户帐户。
I don't know how to access the user's account on cloudkit.
我的解决方案是检索用户的AppleID,查询Cloudkit并修改用户的属性...但是我想应该有更好的方法来做到这一点。他们不是像NSUserDefault那样的东西,而是用于cloudkit吗?
My solution would be to retrieve user's AppleID, query Cloudkit and modify the user's attribute... But I guess there should be a better way to do it. Isn't their something like NSUserDefault but for cloudkit?
推荐答案
首先,您必须找出当前登录的userId是什么。您可以通过在容器上执行fetchUserRecordIDWithCompletionHandler方法来实现。然后,可以通过在该容器上执行discoverUserInfoWithUserRecordID来获取更多(当前仅姓氏和姓氏)信息。
You first have to find out what the current loged in userId is. You can do that by executing the fetchUserRecordIDWithCompletionHandler method on the container. Then you can get more (currently only first and last name) information by executing the discoverUserInfoWithUserRecordID on that container.
您可以使用其他自定义字段扩展Users表,但我不建议这样做。 Users表是一个特殊的系统表,有一些限制。最好使用用户设置创建单独的recordType。只需在用户表中添加CKReference以方便访问。
You could extend the Users table with extra custom fields, but I would not advice doing that. The Users table is a special system table with some limitations. It would be better to create a separate recordType with the user settings. Just add a CKReference to the users table for easy access.
您还必须注意,用户在操作过程中可以注销并使用其他iCloud帐户登录您的应用程序。您可以在应用程序启动后立即执行以下代码来捕获此问题。当然,您必须在看到NSLog的地方实现自己的逻辑
You also have to be aware that a user could log out and log in with a different iCloud account during the operation of your app. You could capture this by executing the code below right after your application start. Of course you have to implement your own logic where you see the NSLog
var ubiquityIdentityDidChangeNotificationToken = NSNotificationCenter.defaultCenter().addObserverForName(NSUbiquityIdentityDidChangeNotification, object: nil, queue: nil) { _ in
NSLog("The user’s iCloud login changed: should refresh all user data.")
}
这篇关于cloudkit:如何访问主要用户的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!