Home setup using the Home kit framework in iOS8

I would like to create an HMHome using the HMHomeManager and addHomeWithName .

 HMHomeManager * myHomeManager; [myHomeManager addHomeWithName:@"My Home" completionHandler:^(HMHome *home, NSError *error) { if (!error) { NSLog(@"Created Home : %@",home.name); } else { NSLog(@"Error : %@",[error localizedDescription]); } }]; 

When the application starts, I get the following error instead of creating Home

 Error : The operation couldn't be completed. (HMErrorDomain error -70892.). 
+1
source share
1 answer

Make your class a delegate of HMHomeManager:

 import UIKit import HomeKit class HomeManagerViewController: UITableViewController, HMHomeManagerDelegate { var homeViewController: HomeViewController? = nil var myHomeManager:HMHomeManager? = nil var homes = [AnyObject]() // datasource for tableview 

Your HMHomeManager must be initialized first (you mentioned that you already did this), and your class must be its delegate.

 override func viewDidLoad() { super.viewDidLoad() myHomeManager = HMHomeManager() myHomeManager!.delegate = self 

You add Home to any function you need (for example, when the user presses the + button to insert a new house into the list of tables)

HMHomeManager must have time to connect to the homekit database

 func insertNewObject(sender: AnyObject) { myHomeManager!.addHomeWithName( uniqueHomeName , completionHandler: { (home: HMHome!, error: NSError!) -> Void in if (error != nil) { // adding the home failed; check error for why NSLog("Error : %@",[error.localizedDescription]) } else { // success! // Insert home at bottom of datasource array and bottom of tableview cells self.homes.append(home) let indexPath = NSIndexPath(forRow: self.homes.count-1, inSection: 0) self.tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic) } }) } 

Then you update your tableviews data source in the delegate method homeManagerDidUpdateHomes.

This function is called when the HMHomeManager has completed initialization and will provide you with an array of any previously added houses.

  // #pragma mark - HMHomeManager Delegate func homeManagerDidUpdateHomes(manager: HMHomeManager!) { self.homes = manager!.homes self.tableView.reloadData() } 

At the first launch of the application, a request for access to its " Accessory data " should appear.

Make sure you click โ€œOKโ€ for this.

Accessory data

Also: add "HomeKit" according to your application rights:

  • Select your application Purpose .
  • Select the Features tab.
  • Switch " HomeKit " to "On".
  • Enter developer ID, etc.

attached image example

Adding HomeKit Entitlements

+6
source

Source: https://habr.com/ru/post/974398/


All Articles