2016-10-08 60 views
0

我的應用程序未打開HealthKit權限屏幕,用戶可以在其中允許訪問Healthkit。 我收到手錶上的屏幕顯示我的應用程序「想要訪問您的健康數據」。但手機只是移動到應用程序啓動屏幕,然後第一頁,而不是提出設置。沒有錯誤。該應用編碼如下以請求授權。有任何想法嗎?HealthKit權限不打開

在電話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 

    } 
} 

回答

2
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,並描述說爲什麼你需要訪問你的應用程序。

希望這會幫助你。