2016-11-03 120 views
1

我實際試圖訪問用戶的生物日期和bioSex之前確定授權。但它在模擬器上工作,但不是在iphone和配對手錶上。蘋果健康工具包錯誤域= com.apple.healthkit代碼= 5「授權未確定」

let birthdayType = HKQuantityType.characteristicType(forIdentifier: HKCharacteristicTypeIdentifier.dateOfBirth) 
    let biologicalSexType = HKQuantityType.characteristicType(forIdentifier: HKCharacteristicTypeIdentifier.biologicalSex) 
    let quantityType = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.heartRate) 
    let readDataTypes: Set<HKObjectType> = [quantityType!, birthdayType!, biologicalSexType!] 
    guard HKHealthStore.isHealthDataAvailable() == true else { 
     label.setText("not available") 
     return 
    } 

    let readDataTypes: Set<HKObjectType> = self.dataTypesToRead() 

    healthStore.requestAuthorization(toShare: nil, read: readDataTypes) { (success, error) -> Void in 
     if success == false { 
      self.displayNotAllowed() 
     } 
    } 

    var birthDay: Date! = nil 
    do { 
     birthDay = try self.healthStore.dateOfBirth() 
    } catch let error as NSError{ 
     print("Either an error occured fetching the user's age information or none has been stored yet. \(error)") 
     return -1 
    } 
    var biologicalSex:HKBiologicalSexObject! = nil 
    do { 
     try biologicalSex = self.healthStore.biologicalSex() 
    }catch let error as NSError{ 
     print("Failed to read the biologicalSex! \(error)") 
    } 

回答

1

這是一個併發問題,而不是HealthKit問題。

requestAuthorization可能會顯示一個對話框並在後臺進程中給出結果。

你必須等待requestAuthorization的答案,然後再繼續讀生日和bioSex。

+0

謝謝。我已經解決了這個問題。我的watchOS版本是2.2.1。它無法在watch和IOS之間同步healthstore的數據。我更新了我的watchOS版本。是工作 : ) – Maxwell