简体   繁体   中英

Setting up home using Home kit framework in iOS8

I would like to create HMHome with 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 is run I am getting the following error instead of creating Home

Error : The operation couldn’t be completed. (HMErrorDomain error -70892.).

Make your class the HMHomeManager delegate:

import UIKit
import HomeKit

class HomeManagerViewController: UITableViewController, HMHomeManagerDelegate {

    var homeViewController: HomeViewController? = nil
    var myHomeManager:HMHomeManager? = nil

    var homes = [AnyObject]()   // datasource for tableview

Your HMHomeManager has to be initialised first ( you've mentioned that you've already done this), and your class set to be its delegate.

override func viewDidLoad() {
    super.viewDidLoad()

    myHomeManager = HMHomeManager()
    myHomeManager!.delegate = self

You Add the Home in whatever function you want (ie when user taps the "+" button to insert a new home into a tableview list)

The 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)
        }
    })
}

You then update your tableviews datasource in the homeManagerDidUpdateHomes delegate method.

This function is called when HMHomeManager finished initialising and will give you an array of any previously added homes

    // #pragma mark - HMHomeManager Delegate

func homeManagerDidUpdateHomes(manager: HMHomeManager!) {

    self.homes = manager!.homes

    self.tableView.reloadData()
}

When the app is first run there should be a request to access its " Accessory Data ".

Make sure you tap "OK" for this.

配件资料

Also: Add "HomeKit" under your apps entitlements:

  1. Choose your app Target .
  2. Select the " Capabilities " tab.
  3. Switch " HomeKit " to "On".
  4. Enter your developer Id, etc.

example image attached

添加HomeKit权利

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM