(Swift) iOS application crashes before asking for HomeKit Access

372 views Asked by At

In my iOS application, when I delete the app off of my phone and rebuild it, when I launch it the app crashes from trying to access the HMHomeManager variables I have set up, and THEN asks for permission for HomeKit access.

Is there anything I can do to have the app ask for permission for HomeKit, before it crashes?

I tried putting this in my appDelegate file.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    //push notifications
    registerPushNotifications()

    let home = HMHomeManager()
    home.accessibilityActivate()

    return true
}

But it's not working. Thanks for the help!

edit:

This is the code that crashes, this is called when the first view controller loads.

//adding the accessories
func addAcessories() {
    for accessory in (homeManager.primaryHome?.accessories)! { // <-- crashes right here
        if accessory.isBridged == true {
            devices.append(accessory)
            print(accessory.name)
        }
    }
    print("")
}

edit: When it crashes, it gives me (lldb) in the console, and Thread 1: EXC_BREAKPOINT in the text editor.

I've also tried putting this in my appDelegate file. I've tried making appDelegate conform to HMHomeManagerDelegate

class AppDelegate: UIResponder, UIApplicationDelegate, HMHomeDelegate, HMHomeManagerDelegate {

and calling this in my ApplicationDidFinishLoadingWithOptions method.

 let home = HMHomeManager()
    home.accessibilityActivate()

    home.delegate = self

    homeKit.addAcessories()
    homeKit.addCharacteristics()

    return true
1

There are 1 answers

11
Timmy On

You have to add NSHomeKitUsageDescription into your info.plist to prevent the crash.

This will automatically ask for permission. Also, you have to add a description for it as well, as of iOS 10. You can just say "(APP NAME) needs access to the home kit" but you can make it what ever you want.

Edit: Try removing the HomeKit code from your appDelegate