HMHomeManager didAddHome not returned

2019-08-12 03:21发布

问题:

I am having a problem with adding homes with HMHomeManager. I can call the function add well, but HMHomeManager does not return

func homeManager(manager: HMHomeManager!, didAddHome home: HMHome!) {
    println("\(__FUNCTION__)")
}

I am sure I already assigned the delegate of homeManager. Below is my code:

class ViewController: UIViewController, HMHomeManagerDelegate {
var manager: HMHomeManager

required init(coder aDecoder: NSCoder) {
    manager = HMHomeManager()
    super.init(coder: aDecoder)
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    manager.delegate = self
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Actions

@IBAction func home1Pressed(sender: UIButton) {
    manager.addHomeWithName("Home12", completionHandler: {
        (home:HMHome!, error:NSError!) -> Void in
        if error != nil {
            println("error: \(error)")
        }
        else {
            println("no error")
        }
    })
}

@IBAction func home2Pressed(sender: UIButton) {
    manager.addHomeWithName("Home23", completionHandler: {
        (home:HMHome!, error:NSError!) -> Void in
        if error != nil {
            println("error: \(error)")
        }
        else {
            println("no error")
        }
    })
}

// MARK: - HMHomeManagerDelegate

func homeManagerDidUpdateHomes(manager: HMHomeManager!) {
    println("\(__FUNCTION__)")
}

func homeManagerDidUpdatePrimaryHome(manager: HMHomeManager!) {
    println("\(__FUNCTION__)")
}

func homeManager(manager: HMHomeManager!, didAddHome home: HMHome!) {
    println("\(__FUNCTION__)")
}

func homeManager(manager: HMHomeManager!, didRemoveHome home: HMHome!) {
    println("\(__FUNCTION__)")
}

}

Note that: when I first run this code, homeManagerDidUpdateHomes is called which proves to me that homeManager.delegate=self is correct

In this code, after I pressed button1 (home1Pressed triggered) and button2 (home1Pressed triggered), home12 and home23 are created. I know this because next time I run this code, the output is:

homeManagerDidUpdateHomes
homes: [[ name = Home1, primary : Yes ], [ name = Home12, primary : No ], [ name = Home23, primary : No ]]

But Why homeManager(manager: HMHomeManager!, didAddHome home: HMHome! is not called when I pressed button1 and button2? I cannot figure it out, for 2 days.

Thanks,

回答1:

Did you remember to enable HomeKit in Capabilities?



回答2:

Quoting from Apple Dev Forum discussions:

This is not a bug but yeah, it should be better documented.

All HomeKit's delegates will only get invoked when there are changes in HomeKit database that are not caused by the app or doesn't have a direct connection to your completion handler (Like Synced from iCloud, another HomeKit app added a room then user switched to your app)



回答3:

I get the same error. As a workaround, I am getting the homeManagerDidUpdatePrimaryHome callback. I filed a bug for this during the beta, which was closed as a duplicate, but I guess it needs to be refiled. homeManagerDidUpdatePrimaryHome is working, but that really only helps with the first home.

I re-reported this to Apple.



回答4:

This is an iOS bug (tested with 8.3).

21097472 HomeKit API - HMHomeManagerDelegate didAddHome is not called