HealthKit authorization crashes with unhandled NSException in iOS 10 beta 1
Asked Answered
D

2

10

Using iOS 10, first beta, HealthKit authorization crashes. With code that was running with iOS 9.x (except that I changed to Swift 3)

even the most simple authorization crashes:

func authorizeHealthKit(_ completion: ((success:Bool, error:NSError?) -> Void)!)
{
    // 1. Set the types you want to read from HK Store
    var healthKitTypesToRead: Set<HKObjectType> = Set<HKObjectType>()
    healthKitTypesToRead.insert(HKObjectType.characteristicType(forIdentifier: HKCharacteristicTypeIdentifier.dateOfBirth)!)

    // 2. Set the types you want to write to HK Store
    var healthKitTypesToWrite: Set<HKSampleType> = Set<HKSampleType>()

    // 3. If the store is not available (for instance, iPad) return an error and don't go on.
    if !HKHealthStore.isHealthDataAvailable()
    {
        // do some error handling
        return;
    }


    // 4.  Request HealthKit authorization

    // iOS 10 beta 1 throws NSException without declaring it:

    healthStore.requestAuthorization(toShare: healthKitTypesToWrite, read: healthKitTypesToRead) { (success: Bool, error: NSError?) -> Void in
        // do stuff
    }
}

this is the simplest code that crashes in iPhone SE simulator with iOS 10 beta 1.

The exception message is

"libc++abi.dylib: terminating with uncaught exception of type NSException".

Is it possible that authorization does not work at all with iOS 10 beta 1? This is XCode 8 beta 1

What works: my HelthKit App that I built using Xcode 7.3 with iOS 9.3 target runs fine under iOS 10 beta 1 on hardware iPhone 5.

Dissipate answered 15/6, 2016 at 17:17 Comment(4)
Sorry, have no idea what the solution is here but just wanted to echo that I'm having the same issue in Objective C. Seems to work fine on the Watch Simulator but unfortunately access to Health data on the watch requires authorisation on the iPhone app first. Im using the iPhone app simulator and i get the same crash. Could this be a beta 1 bug? Though its not mentioned in the release notes.Adalie
Did you manage to resolve your issue? On the latest (Beta 6), I get an internal error as soon as I put in the brackets of the completion handler in healthStore.requestAuthorizationHaberdasher
Yes, the accepted answer is the solutionDissipate
By the way, there are other causes, that lead to the same crash: for example, if you try to get write access for exercise minutes you get exactly the same crash without any text.Dissipate
I
15

The message of the exception should give you a hint about what the issue is. Starting in iOS 10, usage strings that describe why your app would like access to the users HealthKit data are required. You can specify them in your app's Info.plist.

Inspector answered 16/6, 2016 at 14:42 Comment(3)
Thanks for that hint. There is no message at compile time, which would be the best if it is required, The exception message is "libc++abi.dylib: terminating with uncaught exception of type NSException", when I try to catch the exception, the compiler says that catch code can never be reached. The NSError return parameter of the method is never used.Dissipate
Is this text shown to the end user, which means plist has to be translated, or is this text for the app reviewer?Dissipate
It looks like the SDK is not printing the the localized description of the exception. Yes, the string will be presented to the user and you can localize it.Inspector
D
7

From the Apple Documentation:

An iOS app linked on or after iOS 10.0 must include in its Info.plist file the usage description keys for the types of data it needs to access or it will crash. To access and update HealthKit data specifically, it must include theNSHealthShareUsageDescription and NSHealthUpdateUsageDescription keys, respectively.

Dispel answered 18/6, 2016 at 15:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.