从SWIFT到Objective C" healthStore.requestAuthorizationToShareTypes"

时间:2015-09-01 20:36:40

标签: objective-c swift health-kit

不幸的是,Apple没有将他们的新示例转换为Objective C.我有一个有效的SWIFT代码片段,但是我对目标C的翻译不起作用 - 授权请求没有出现在iPhone上的目标c代码中 -

(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

1 个答案:

答案 0 :(得分:1)

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) {
相关问题