objective-c - 需要从 SWIFT 到 Objective C 的转换帮助 "healthStore.requestAuthorizationToShareTypes"

标签 objective-c swift healthkit

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

环球银行金融电信协会:

   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

最佳答案

您在使用之前缺少创建healthScore

let healthStore = HKHealthStore() 创建一个实例。

// Missing initialization
self.healthScore = [HKHealthStore new];

...

[self.healthScore requestAuthorizationToShareTypes:nil readTypes:dataTypes completion:^(BOOL success, NSError * _Nullable error) {

关于objective-c - 需要从 SWIFT 到 Objective C 的转换帮助 "healthStore.requestAuthorizationToShareTypes",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32340443/

相关文章:

iphone - Objective-C 中的 NSDate 和 dateString

ios - 如何在状态之间正确设置 UIButton 的动画?

ios - 如何获取我的 iOS 应用程序的本地化语言?

ios - 我可以在没有 HealthKit 的情况下使用 ResearchKit 吗?

ios - HKHealthStore 类型的值没有类型 dateOfBirthWithError

Objective-C:如何提取字符串的一部分(例如从 '#' 开始)

swift - 利用 Swift 实现此排序集算法的更惯用方法是什么?

ios - 在 firestore 中存储字典值时存在逻辑问题

ios - Swift ResearchKit HealthKit QuestionStep

ios - 如何在 ARC 上发布 GADBannerView?