apple-watch - 当向 Apple Watch 请求健康套件许可时,requestAuthorizationToShareTypes 函数完成 block 不会被调用

标签 apple-watch healthkit

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

最佳答案

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

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

关于apple-watch - 当向 Apple Watch 请求健康套件许可时,requestAuthorizationToShareTypes 函数完成 block 不会被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33895126/

相关文章:

xamarin.forms - 我们可以将 Apple Watch 扩展添加到 xamarin 表单应用程序(跨平台应用程序)吗?

ios - 无法在 watchOS 上使用 XCFramework

audio - 如何控制 Apple Watch 蓝牙音频的音量

swift - 在 Series 5 Watch 和 Watch OS6 上使用 `HKAnchoredObjectQuery` 返回心率值时出现问题

ios - 使用 Healthkit 获取和设置医疗 ID 中的信息?

ios - 如何在 HealthKit 中获取 `addQuantitiesFromSamples`?

ios - HealthKit:保存在 iPhone 上的锻炼在 Activity App 中不可见

ios - 通过 HRV 跟踪节拍以节拍心率 Apple Watch

ios - 在 Apple Watch 上移动 WKInterfaceImage

ios - HealthKit 和配件没有配套的 iOS 应用程序