Reputation: 2095
Unfortunately Apple do not translate their new examples to Objective C. I have a working SWIFT code fragment, but my translation to objective C is not working - The authorisation request does not appear in the objective c code on the iPhone -
SWIFT:
class InterfaceController: WKInterfaceController {
let healthStore = HKHealthStore()
override func willActivate() {
super.willActivate()
guard let quantityType = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate) else {
return
}
let dataTypes = Set(arrayLiteral: quantityType)
healthStore.requestAuthorizationToShareTypes(nil, readTypes: dataTypes) { (success, error) -> Void in
if success == false {
}
}
}
}
Objective-C:
@interface InterfaceController()
@property HKHealthStore * healthScore;
@end
@implementation InterfaceController
- (void)willActivate {
[super willActivate];
NSString * quantity = HKQuantityTypeIdentifierHeartRate;
HKQuantityType * quantityType = [HKQuantityType quantityTypeForIdentifier:quantity];
NSSet <HKQuantityType *> * dataTypes = [NSSet setWithArray:@[quantityType]];
[self.healthScore requestAuthorizationToShareTypes:nil readTypes:dataTypes completion:^(BOOL success, NSError * _Nullable error) {
if (!success) { } }];
}
@end
Upvotes: 0
Views: 471
Reputation: 112857
You are missing creating healthScore
prior to using it.
let healthStore = HKHealthStore()
creates an instance.
// Missing initialization
self.healthScore = [HKHealthStore new];
...
[self.healthScore requestAuthorizationToShareTypes:nil readTypes:dataTypes completion:^(BOOL success, NSError * _Nullable error) {
Upvotes: 1