Voglio verificare se HeathKit è stato autorizzato a leggere i dati dell'utente, se sono autorizzato a seguire gli allenamenti, se non inserire un avviso. Ma requestAuthorizationToShareTypes sembra sempre restituire vero? Come posso ottenere un riferimento se l'utente mi ha autorizzato o meno?Come verificare se HealthKit è stato autorizzato
override func viewDidLoad() {
super.viewDidLoad()
//1. Set the types you want to read from HK Store
let healthKitTypesToRead: [AnyObject?] = [
HKObjectType.workoutType()
]
//2. If the store is not available (for instance, iPad) return an error and don't go on.
if !HKHealthStore.isHealthDataAvailable() {
let error = NSError(domain: "com.myndarc.myrunz", code: 2, userInfo: [NSLocalizedDescriptionKey: "HealthKit is not available in this Device"])
print(error)
let alertController = UIAlertController(title: "HealthKit Not Available", message: "It doesn't look like HealthKit is available on your device.", preferredStyle: .Alert)
presentViewController(alertController, animated: true, completion: nil)
let ok = UIAlertAction(title: "Ok", style: .Default, handler: { (action) -> Void in })
alertController.addAction(ok)
}
//3. Request Healthkit Authorization
let sampleTypes = Set(healthKitTypesToRead.flatMap { $0 as? HKSampleType })
healthKitStore.requestAuthorizationToShareTypes(sampleTypes, readTypes: nil) {
(success, error) -> Void in
if success {
dispatch_async(dispatch_get_main_queue(), {() -> Void in
self.performSegueWithIdentifier("segueToWorkouts", sender: nil)
});
} else {
print(error)
dispatch_async(dispatch_get_main_queue(), {() -> Void in
self.showHKAuthRequestAlert()
});
}
}
}
In alternativa, ho provato authorizationStatusForType e acceso i suoi valori enum ma ha avuto lo stesso problema in che sto sempre autorizzato.
il sistema non hai chiesto il permesso? –
no, ma anche se non lo autorizzi, il blocco di successo si chiama – GarySabo