我正在尝试将 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()
}
}
希望任何人有一个想法,可以帮助我。谢谢你。