swift - HealthKit 权限未打开

标签 swift healthkit

我的应用程序没有打开用户可以允许访问 Healthkit 的 HealthKit 权限屏幕。 我的 watch 屏幕显示我的应用程序“想要访问您的健康数据”。但是手机只是移动到应用程序启动屏幕,然后是第一页,而不是调出设置。没有错误。该应用程序编码如下以请求授权。有任何想法吗?

在手机AppDelegate中:

import UIKit
import WatchConnectivity
import HealthKit


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, WCSessionDelegate {

var window: UIWindow?
let healthStore = HKHealthStore()


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    WatchSessionManager.sharedManager.startSession()


    return true
}


// authorization from watch
func applicationShouldRequestHealthAuthorization(application: UIApplication) {

    let healthStore = HKHealthStore()

    let quantityType = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)

    let calorieQuantityType = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned)

    let dataTypesToRead = NSSet(objects: HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)!, HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned)!)
   healthStore.requestAuthorizationToShareTypes(nil, readTypes: dataTypesToRead as! Set<HKObjectType>, completion: { (success, error) in
        if success {
        } else {
            print(error!.description)
        }
        })

    healthStore.handleAuthorizationForExtensionWithCompletion { success, error in

    }
}

最佳答案

    let healthKitStore:HKHealthStore = HKHealthStore()

        func authorizeHealthKit(completion: ((_ success:Bool, _ error:NSError?) -> Void)!)
        {   
            //check app is running on iPhone not other devices(iPad does not have health app)
            if !HKHealthStore.isHealthDataAvailable()
            {
                let error = NSError(domain: "DomainName.HealthKitTest", code: 2, userInfo: [NSLocalizedDescriptionKey:"HealthKit is not available in this Device"])
                if completion != nil
                {
                    completion(false, error)
                }
                return;
            }

            healthKitStore.requestAuthorization(toShare: healthKitTypeToWrite() as? Set<HKSampleType>, read: nil) { (success, error) -> Void in

                if completion != nil
                {
                    print("Success: \(success)")
                    print("Error: \(error)")
                    completion?(success, error as NSError?)
                }
            }
        }

        func healthKitTypeToWrite() -> NSSet {
            let typesToWrite = NSSet(objects: HKQuantityType.quantityType(forIdentifier: .dietaryFatTotal),
                                              HKQuantityType.quantityType(forIdentifier: .dietaryFatSaturated),
                                              HKQuantityType.quantityType(forIdentifier: .dietaryCarbohydrates),
                                              HKQuantityType.quantityType(forIdentifier: .dietarySugar),
                                              HKQuantityType.quantityType(forIdentifier: .dietaryProtein),
                                              HKQuantityType.quantityType(forIdentifier: .dietarySodium))
            return typesToWrite
        }

并确保将“NSHealthUpdateUsageDescription”添加到您的 info.plist 和描述中,说明为什么您需要访问您的应用。

希望对您有所帮助。

关于swift - HealthKit 权限未打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39927755/

相关文章:

ios - 我如何对 Musik 的开关和按钮进行编程?

ios - 我需要 Apple Watch 应用程序才能在 iPhone 和 Apple Watch 之间进行通信吗?

ios - 如何在其他类中使用导入的HealthKit样本

ios - 没有更多上下文的表达式类型不明确 Healthkit ios Swift

ios - 函数 getWiFiAddress() 总是返回 nil?

arrays - Swift - 检查字典中是否存在数组

swift - 可选的无主引用与 Swift 5.0 中的弱引用

ios - VNCoreMLFeatureValueObservation 是否输出 softmax 概率?如果是这样,如何提取最高值?

swift - 查找 swift 框架中的新增功能

ios - HKObserverQuery、后台更新和隐私政策