創建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)屬性來獲取用戶名。