首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未打开HealthKit权限

未打开HealthKit权限
EN

Stack Overflow用户
提问于 2016-10-08 09:23:17
回答 2查看 1.3K关注 0票数 1

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

在电话AppDelegate中:

代码语言:javascript
复制
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

    }
}
EN

回答 2

Stack Overflow用户

发布于 2016-10-14 18:40:06

代码语言:javascript
复制
    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
        }

一定要在你的info.plist和描述中添加'NSHealthUpdateUsageDescription‘,说明为什么你需要访问你的应用程序。

希望这能对你有所帮助。

票数 4
EN

Stack Overflow用户

发布于 2019-02-02 00:45:52

在我的info.plist中添加NSHealthUpdateUsageDescription解决了这个问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39927755

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档