4

I have an iOS application using HomeKit and I need to check if application can access HomeKit Store. As I noticed, homeManagerDidUpdateHomes will be called anyway. How do I check for HomeKit store permission?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Nikita Zernov
  • 5,465
  • 6
  • 39
  • 70

3 Answers3

3

The Swift 4.2 version of Volodymyr's hack/answer.

import UIKit
import HomeKit

class HomeKitAccessViewController: UIViewController {

    let manager = HMHomeManager()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.manager.delegate = self

    }

}

extension HomeKitAccessViewController: HMHomeManagerDelegate {

    func homeManagerDidUpdateHomes(_ manager: HMHomeManager) {
        let number = manager.value(forKey: "_didUpdateHomes")

        if let num = number, let boolValue = num as? Bool {
            if boolValue == true {
                print("We got access.")
            }else{
                print("We don't have access")
            }
        }
    }
}
Ivan Le Hjelmeland
  • 1,065
  • 11
  • 26
2

I have a hack for this problem which is working for me:

Init HMHomeManager

self.manager = [[HMHomeManager alloc] init];
self.manager.delegate = self;

Check private property in

-(void)homeManagerDidUpdateHomes:(HMHomeManager *)manager {

  //HACK to check that the application does not have HomeKit permission
  NSNumber *private = [self.manager valueForKey:@"_didUpdateHomes"];


  if (private && ![private boolValue]) {
    //Warning to user
    return; 
  }
  // Do other logic
}

I hope Apple provides us API for this soon

0

Edit: This apparently doesn't work anymore. Unfortunately, I'm not doing HK development anymore. See @windwalker's comment below for some info.

[[HMHomeManager alloc] init]

will return nil if you don't have access to the home. Other HomeKit inits will also return nil, that's just the one that I'm checking since it's the first thing that we try once the app loads.

Adam Shiemke
  • 3,734
  • 2
  • 22
  • 23
  • 2
    Not working (at least in ios 10). You can check the "homes" property of HMHomeManager. If there is a least one HMHome, you have the permission but if there is no home, it may be: You have never asked the user The user refused permission You have the permission but you have actually no Home in your homekit environment – The Windwaker Oct 04 '16 at 15:28