讓您的課HMHomeManager委託:
import UIKit
import HomeKit
class HomeManagerViewController: UITableViewController, HMHomeManagerDelegate {
var homeViewController: HomeViewController? = nil
var myHomeManager:HMHomeManager? = nil
var homes = [AnyObject]() // datasource for tableview
你HMHomeManager必須首先初始化(你已經提到你已經做到了這一點),並且你的班級設置爲它的代表。
override func viewDidLoad() {
super.viewDidLoad()
myHomeManager = HMHomeManager()
myHomeManager!.delegate = self
您添加任何你想要的功能,主頁(即當用戶點擊「+」按鈕,插入一個新的家變成一個實現代碼如下列表)
的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)
}
})
}
然後,您在homeManagerDidUpdateHomes委託方法中更新您的tableviews數據源。
時HMHomeManager完成初始化這個函數被調用,並給你任何先前添加的家園數組
// #pragma mark - HMHomeManager Delegate
func homeManagerDidUpdateHomes(manager: HMHomeManager!) {
self.homes = manager!.homes
self.tableView.reloadData()
}
當應用程序第一次運行應該有訪問其「附件數據」的請求。
確保點擊「確定」。
![Accessory Data](https://i.stack.imgur.com/iz743.png)
另外:添加 「HomeKit」 在你的應用程序的權利:
- 選擇你的應用程序目標。
- 選擇「功能」選項卡。
- 開關「HomeKit」到「開」。
- 輸入您的開發者ID等
例如圖像連接
![Adding HomeKit Entitlements](https://i.stack.imgur.com/iQBMS.png)
僅供參考,你可以找到在HMErrorDomain類錯誤的標籤。你的是:HMErrorUnconfiguredParameter(-70892)。不知道它是什麼意思...你的代碼不會在'myHomeManager'上顯示'alloc] init'... –
HMHomeManager * myHomeManager = [HMHomeManager alloc] init];即使我已經將它包含在我的應用程序中,但我收到了錯誤。 – user3400046
如果您@ user3400046同意下面的答案,建議您接受該答案。 – Kamarshad