多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
创建home的用户是该home的管理员,可以执行所有操作,包括添加一个客人用户到home。任何管理员添加到这个home的用户(HMUser)都有一个有限的权限。客人不能更改家庭的布局,但是可以执行下面的动作: * 识别智能电器 * 读写特性 * 观察特性值变化 * 执行动作集 比如,一个家庭的户主可以创建一个home布局并向其中添加家庭成员。每个家庭成员必须拥有一个iOS设备和Apple ID以及相关的iCloud账户。iCloud需要个人输入的Apple ID和户主提供的Apple ID相吻合,以便让他们访问这个home。考虑到隐私问题,Apple ID对你的App是不可见的。 管理员需要遵从以下步骤来添加一个客人到home中: 1\. 管理员调用一个动作将客人添加到home中。 2\. 你的App调用[addUserWithCompletionHandler:](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMHome_Class/index.html#//apple_ref/occ/instm/HMHome/addUserWithCompletionHandler:)异步方法。 3\. HomeKit展示一个对话框,要求输入客人的Apple ID。 4\. 用户输入客人的Apple ID。 5\. 在完成回调中返回一个新的用户。 6\. 你的App展示客人的名字。 添加一个客人到home,需要在客人的iOS设备上做以下操作: 1\. 用户在iCloud偏好设置中输入iCloud凭证(Apple ID和密码)。 2\. 用户启动你的App。 3\. 你的App通过home manager object获得一个home集合。 4\. 如果iCloud的凭证和管理员输入的Apple ID相同,那么管理员的home将会出现在homes属性中。 客人执行的操作可能会失败。如果一个异步方法中出现[HMErrorCodeInsufficientPrivileges](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HomeKit_Constants/index.html#//apple_ref/c/econst/HMErrorCodeInsufficientPrivileges)错误码的话,这就意味着用户没有足够的权限来执行动作-也许这个用户只是客人,而不是管理员。 为了测试你的App是否正确处理了客人用户,请阅读[Testting Multiple iOS Devices and Users](https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/HomeKitDeveloperGuide/TestingYourHomeKitApp/TestingYourHomeKitApp.html#//apple_ref/doc/uid/TP40015050-CH7-SW12)。 **添加和移除用户** 为了添加一个客人用户到home,请使用[addUserWithCompletionHandler:](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMHome_Class/index.html#//apple_ref/occ/instm/HMHome/addUserWithCompletionHandler:)异步方法。 ~~~ [self.home addUserWithCompletionHandler:^(HMUser *user, NSError *error) { if (error == nil) { // Successfully added a user } else { // Unable to add a user } }]; ~~~ 想要移除home中的用户,请使用[HMHome](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMHome_Class/index.html#//apple_ref/occ/cl/HMHome)类的[removeUser:completionHandler:](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMHome_Class/index.html#//apple_ref/occ/instm/HMHome/removeUser:completionHandler:)方法。 通过实现[HMHomeDelegate](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMHomeDelegate_Protocol/index.html#//apple_ref/occ/intf/HMHomeDelegate)协议中的[home:didAddUser:](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMHomeDelegate_Protocol/index.html#//apple_ref/occ/intfm/HMHomeDelegate/home:didAddUser:)和[home:didRemoveUser:](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMHomeDelegate_Protocol/index.html#//apple_ref/occ/intfm/HMHomeDelegate/home:didRemoveUser:)协议方法检查新添加和移除的用户并更新视图。关于如何创建一个delegate,请阅读[Observing Changes to Individual Homes](https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/HomeKitDeveloperGuide/RespondingtoHomeKitDatabaseChanges/RespondingtoHomeKitDatabaseChanges.html#//apple_ref/doc/uid/TP40015050-CH5-SW4)。 **获得用户名** 出于隐私的考虑,你的app对用户名只有读得权限,并不能读写用户的Apple ID。使用[HMHome](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMHome_Class/index.html#//apple_ref/occ/cl/HMHome)对象的[users](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMHome_Class/index.html#//apple_ref/occ/instp/HMHome/users)属性来获取用户。使用[HMUser](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMUser_Class/index.html#//apple_ref/occ/cl/HMUser)类的[name](https://developer.apple.com/library/ios/documentation/HomeKit/Reference/HMUser_Class/index.html#//apple_ref/occ/instp/HMUser/name)属性来获取用户名。