HealthKit 授权在 iOS 10 beta 1 中因未处理的 NSException 而崩溃

HealthKit authorization crashes with unhandled NSException in iOS 10 beta 1

使用iOS 10,第一个测试版,HealthKit 授权崩溃。 使用 运行 iOS 9.x 的代码(除了我更改为 Swift 3)

即使是最简单的授权也会崩溃:

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
    }
}

这是在 iPhone SE 模拟器中崩溃的最简单的代码 iOS 10 beta 1。

异常消息是

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

授权是否可能对 iOS 10 beta 1 根本不起作用? 这是 XCode 8 测试版 1

什么有效:我使用 Xcode 7.3 和 iOS 9.3 目标构建的 HelthKit 应用程序在硬件 iPhone 5 上的 iOS 10 beta 1 下运行良好。 =13=]

异常消息应该会提示您问题是什么。从 iOS 10 开始,需要 描述您的应用为何想要访问用户 HealthKit 数据的使用字符串。您可以在应用的 Info.plist.

中指定它们

来自 Apple 文档:

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.