मैं यह जांचना चाहता हूं कि क्या मुझे हेथकिट को उपयोगकर्ता के डेटा को पढ़ने के लिए अधिकृत किया गया है, अगर मैं चेतावनी पॉप नहीं करता हूं, तो अगर मैं चेतावनी पॉप नहीं करता हूं। लेकिन अनुरोध प्राधिकरण ToShareTypes हमेशा सच वापस लगता है? मैं इस बात का संदर्भ कैसे प्राप्त कर सकता हूं कि उपयोगकर्ता ने मुझे अधिकृत किया है या नहीं?हेल्थकिट को अधिकृत किया गया है या नहीं, यह जांचने के लिए कि
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()
});
}
}
}
वैकल्पिक रूप से, मैं authorizationStatusForType की कोशिश की और उसके enum मूल्यों पर स्विच लेकिन यह है कि मैं हमेशा के लिए अधिकृत कर रहा हूँ में एक ही समस्या थी।
प्रणाली कभी नहीं में अनुमति पहुँच जाँच का एक उदाहरण अनुमति के बारे में आप से पूछा? –
ऐसा नहीं करता है, लेकिन अगर आप इसे अधिकृत नहीं करते हैं, तो सफलता ब्लॉक को – GarySabo