2

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
Jolaroux
  • 361
  • 1
  • 5
  • 15

1 Answers1

1

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

Timmy
  • 537
  • 3
  • 10
  • I have that in there actually, which is what's confusing me. It asks for permission after it crashes, with the message I put in the `info.plist` file. – Jolaroux Dec 13 '16 at 00:27
  • Did u add the key under `Information Property List` and is the key named `Privacy - Home Kit Usage Description`? Also, are you running the app in the simulator or on a device? – Timmy Dec 13 '16 at 00:32
  • Sorry there shouldn't be a space between HomeKit – Timmy Dec 13 '16 at 00:33
  • Yes, the key is in the right place, under `Information Property List`, in `Privacy- HomeKit Usage Description`, and I'm running the app on a device. The issue is just when the app asks for permission. It asks _after_ it attempts to run code, not before, when the app launches. – Jolaroux Dec 13 '16 at 00:36
  • Can you please update your post with the crash message from the console if there is one? – Timmy Dec 13 '16 at 00:39
  • Try removing any break points you have in your project, make your Xcode is up to date, and build and clean your project. That's my advice. – Timmy Dec 13 '16 at 00:50
  • It's not a breakpoint I implemented, it's a crash in the code. A point that the code stopped running and crashed at – Jolaroux Dec 13 '16 at 01:01
  • I wasn't sure because exc_breakpoint also appears when you have a breakpoint I thought you may have forgotten about, if possible, try not using HomeKit code right away in the app delegate. – Timmy Dec 13 '16 at 01:04
  • Ok, when I commented out the code in the app delegate, and the root view controller that calls the home kit code, the permission is asked successfully! But now that gives me the problem of finding where to initialize these home kit variables when the app loads. – Jolaroux Dec 13 '16 at 01:13
  • Well since this is only a first time start up issue, because it's only after you delete the app which removes the previous users options he had set, I would suggest putting it in the first view controller a new user would be greeted with. Possibly like a login screen or a view that's shown after they signed up. You shouldn't worry too much about this considering it'll only be shown once. – Timmy Dec 13 '16 at 01:16
  • I don't understand what I just wrote above but what I meant to say was just the first view controller the user is shown when they load up the app. I have no idea what was going through my mind when writing my comment above lol. – Timmy Dec 13 '16 at 01:22