简体   繁体   English

在iOS8中使用Home kit框架设置主页

[英]Setting up home using Home kit framework in iOS8

I would like to create HMHome with HMHomeManager and addHomeWithName . 我想创建HMHomeHMHomeManageraddHomeWithName

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 运行应用程序时,出现以下错误,而不是创建Home

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

Make your class the HMHomeManager delegate: 使您的类成为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 has to be initialised first ( you've mentioned that you've already done this), and your class set to be its delegate. 必须首先初始化HMHomeManager(您已经提到过已经完成了此操作),并且将类设置为其委托。

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 HMHomeManager必须有时间连接到homekit数据库

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. 然后,您可以在homeManagerDidUpdateHomes委托方法中更新tableviews数据源。

This function is called when HMHomeManager finished initialising and will give you an array of any previously added homes HMHomeManager完成初始化后将调用此函数,并将为您提供任何先前添加的房屋的数组

    // #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: 另外:在您的应用程序权限下添加“ HomeKit”:

  1. Choose your app Target . 选择您的应用目标
  2. Select the " Capabilities " tab. 选择“ 功能 ”标签。
  3. Switch " HomeKit " to "On". 将“ HomeKit ”切换为“开”。
  4. Enter your developer Id, etc. 输入您的开发者ID等。

example image attached 附加示例图片

添加HomeKit权利

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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