使用HomeKit添加HMHome

时间:2014-10-15 18:06:58

标签: ios swift homekit

我正在尝试将Home添加到HomeKitDemoApp。当我第一次在设备或模拟器上安装应用程序时,我得到HomeKitDemoApp喜欢访问我的附件数据的弹出窗口。我按OK它接缝工作,房间被添加到我的tableview。 在我在同一设备或模拟器设备上重建应用程序后,我不会将房间放入我的tableview。现在我收到一个错误:

HomeKitDemoApp[22635:1632134] Add home error:Error Domain=HMErrorDomain Code=49 "The operation couldn’t be completed. (HMErrorDomain error 49.)"

我的代码是:

    import Foundation
import UIKit
import HomeKit

class HomeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, HMHomeManagerDelegate{

    var homeManager:HMHomeManager = HMHomeManager()

    @IBOutlet var homesTableView: UITableView!

    var homes = [AnyObject]()

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
    }


    @IBAction func addHome(sender: AnyObject) {
        let alert:UIAlertController = UIAlertController(title: "Zuhause hinzufügen", message: "Neues Zuhause zu HomeKit hinzufügen", preferredStyle: .Alert)


        alert.addTextFieldWithConfigurationHandler(nil)
        alert.addAction(UIAlertAction (title: "abbruch", style: UIAlertActionStyle.Cancel, handler: nil))
        alert.addAction(UIAlertAction(title: "hinzufügen", style: UIAlertActionStyle.Default, handler:
            {
                (action:UIAlertAction!) in
                let textField = alert.textFields?[0] as UITextField

                //create home with the typed name
                    self.homeManager.addHomeWithName(textField.text, completionHandler:
                        {
                            (home: HMHome!,error )in
                            if let error = error {
                                NSLog("Add home error:\(error)")
                            }else{
                                println("im here in reloaddata")
                                self.homes.append(home)
                                self.homesTableView.reloadData()
                            }
                        }
                    )

        }))

        self.presentViewController(alert, animated: true, completion: nil)

    }

     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("CustomHomeCell", forIndexPath: indexPath) as UITableViewCell
        let home = self.homeManager.homes?[indexPath.row] as HMHome
        cell.textLabel?.text = home.name
        return cell

    }


    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return homes.count
    }
    // #pragma mark - HMHomeManager Delegate

    func homeManagerDidUpdateHomes(manager: HMHomeManager!) {

        self.homes = manager.homes

        self.homesTableView.reloadData()
    }

 }

希望有人有个主意,可以帮助我。 谢谢。

3 个答案:

答案 0 :(得分:0)

看来你的房子太多了。我不确定限制是什么,但重建应用程序时不会删除房屋。它们(以及所有其他homekit数据)保留在与iCloud帐户绑定的共享数据库中。

你创造了多少家?

答案 1 :(得分:0)

加入家的最高限额是10。 Homekit不允许添加超过10个家庭。

答案 2 :(得分:0)

HomeKit将允许最多添加10个家庭。要从iOS设备和iCloud中删除所有家庭数据,请转到设置>隐私> HomeKit并点击“重置HomeKit配置”。从设备中删除应用程序并重新安装它将无法清除HomeKit配置。

HTH