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

问题描述 投票:10回答:2

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

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

异常消息是

“libc ++ abi.dylib:以NSException类型的未捕获异常终止”。

iOS 10 beta 1的授权是否可行?这是XCode 8 beta 1

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

swift health-kit ios10
2个回答
13
投票

异常消息应该提供有关问题的提示。从iOS 10开始,需要使用描述您的应用为什么要访问用户HealthKit数据的用法字符串。您可以在应用的Info.plist中指定它们。


7
投票

来自Apple文档:

在iOS 10.0上或之后链接的iOS应用程序必须在其Info.plist文件中包含其需要访问的数据类型的使用说明密钥,否则将崩溃。要专门访问和更新HealthKit数据,它必须分别包含NSHealthShareUsageDescriptionNSHealthUpdateUsageDescription密钥。

© www.soinside.com 2019 - 2024. All rights reserved.