2016-11-03 4 views
1

Я решил определить авторизацию, прежде чем пытаться получить доступ к дате пользователя в Birth и bioSex. Но это работа на симуляторе. Но не на iphone и парные часы.Apple Health Kit Ошибка Домен = 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 может отображать диалоговое окно и дает результат в фоновом режиме.

Перед тем, как продолжить чтение дня рождения и bioSex, вам нужно подождать ответа requestAuthorization.

+0

спасибо. Я решил проблему. Версия моего watchOS - 2.2.1. Он не смог синхронизировать данные магазина здоровья между часами и IOS. Я обновляю версию своего watchOS. Это работает :) – Maxwell