May 22, 2021 HomeKit App Development guide
The user who created the home is the administrator of the home and can do everything from adding a guest user to the home. A ny user (HMUser) that an administrator adds to this home has a limited permission. Guests cannot change the layout of the home, but they can do the following:
For example, the head of a family can create a home layout and add family members to it. E ach family member must have an iOS device and Apple ID and the associated iCloud account. i Cloud requires an Apple ID entered by the individual to match the Apple ID provided by the head of household to allow them to access the home. Apple IDs are not visible to your app due to privacy concerns.
The administrator needs to follow these steps to add a guest to home:
The administrator calls an action to add the guest to the home.
Your app calls addUserWithCompletionHandler: Asynchronous method.
HomeKit displays a dialog box asking for the guest's Apple ID.
The user enters the guest's Apple ID.
A new user is returned in the completed callback.
Adding a guest to home requires the following on the guest's iOS device:
Users enter iCloud credentials (Apple ID and password) in iCloud preferences.
The user launches your app.
Your app gets a home collection through home manager object.
The operation performed by the guest may fail. If an HMErrorCodeInsufficient Privileges error code appears in an asynchronous method, this means that the user does not have sufficient permissions to perform the action - perhaps the user is a guest, not an administrator.
To test that your app is handling guest users correctly, read Testing Multiple iOS Devices and Users.
To add a guest user to home, use addUserWithCompletionHandler: Asynchronous method.
[self.home addUserWithCompletionHandler:^(HMUser *user, NSError *error) {
if (error == nil) {
// Successfully added a user
}
else {
// Unable to add a user
} }];
To remove users from home, use the HMHome class's removeUser:endHandler: method.
Check newly added and removed users and update views by implementing the home:didAddUser:and home:didRemoveUser: protocol methods in the HMHomeDelegate protocol. For how to create a delegate, read Observing Changes to Individual Homes.
For privacy reasons, your app has only read access to the user name and cannot read or write the user's Apple ID. U se the users property of the HMHome object to get the user. Use the name property of the HMUser class to get the user name.