从Apple Watch要求运行状况工具包的权限时未调用requestAuthorizationToShareTypes函数完成块

Rup*_*aur 2 healthkit apple-watch

let healthKitTypesToRead = Set(arrayLiteral:
        HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierDateOfBirth)!,
        HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)!,
        HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned)!,
        HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDietaryEnergyConsumed)!,
        HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierBiologicalSex)!,
        HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeight)!,
        HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMass)!,
        HKObjectType.workoutType()
    )

    // 2. Set the types you want to write to HK Store
    let healthKitTypesToWrite = Set(arrayLiteral:
        HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)!,
        HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned)!,
        HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDietaryEnergyConsumed)!,

        HKQuantityType.workoutType()
    )

    // 3. If the store is not available (for instance, iPad) return an error and don't go on.
    if !HKHealthStore.isHealthDataAvailable()
    {
        let error = NSError(domain: "com.apple.tutorials.healthkit", code: 2, userInfo: [NSLocalizedDescriptionKey:"HealthKit is not available in this Device"])
        if( completion != nil )
        {
            completion(success:false, error:error)
        }
        return;
    }

    // 4.  Request HealthKit authorization

    healthKitStore.requestAuthorizationToShareTypes(healthKitTypesToWrite, readTypes: healthKitTypesToRead) { (success, error) -> Void in
        if( completion != nil )
        {
            completion(success:success,error:error)
        }
    }
Run Code Online (Sandbox Code Playgroud)

leh*_*058 5

Apple Watch将请求委托给用户的iPhone,因此您需要将以下方法添加到iOS AppDelegate:

func applicationShouldRequestHealthAuthorization(application: UIApplication) {
    let healthStore = HKHealthStore()
    healthStore.handleAuthorizationForExtensionWithCompletion {(success, error) -> Void in
        // Add anything you need here after authorization
    }
}
Run Code Online (Sandbox Code Playgroud)