2
-(void)viewDidAppear:(BOOL)animated
{

[super viewDidAppear:animated];
if ([HKHealthStore isHealthDataAvailable]){
    NSSet *writeDataTypes = [self dataTypesToWrite];
    NSSet *readDataTypes = [self dataTypesToRead];

    [self.healthStore requestAuthorizationToShareTypes:writeDataTypes readTypes:readDataTypes completion:^(BOOL success, NSError *error) {

        NSLog(@"%s",__func__);
        if (!success) {
            NSLog(@"You didn't allow HealthKit to access these read/write data types. In your app, try to handle this error gracefully when a user decides not to provide access. The error was: %@. If you're using a simulator, try it on a device.", error);
            return;
        }

        dispatch_async(dispatch_get_main_queue(), ^{
            // Update the user interface based on the current user's health information.
            NSLog(@"=========================== %s",__func__);
        });
    }];
}


}

requestAuthorizationToShareTypes 不回调完成方法。

4

2 回答 2

4

我有一个类似的问题,权限框没有出现并且没有HKHealthStore正确设置,提前为我修复了它

self.healthStore = [[HKHealthStore alloc] init];
于 2015-07-21T14:35:48.990 回答
2

这是一个示例实现,它返回类型而不是注释部分中描述的字符串。

-(NSSet*)datatypesToWrite {

   NSArray *quantityTypes =
   @[HKQuantityTypeIdentifierHeartRate,
     HKQuantityTypeIdentifierBodyTemperature,
     HKQuantityTypeIdentifierBloodPressureSystolic,
     HKQuantityTypeIdentifierBloodPressureDiastolic,
     HKQuantityTypeIdentifierRespiratoryRate];

   NSMutableArray *hkTypes = [[NSMutableArray alloc] init];

   for (NSString *identifier in quantityTypes) {
     HKQuantityType *quantType =
      [HKObjectType quantityTypeForIdentifier:identifier];
     [hkTypes addObject:quantType];
   }

   // Make sure the types are of the correct style (Quantity, Category, Etc.)
   HKCategoryType *catType =
    [HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierSleepAnalysis];
   [hkTypes addObject:catType];

   return [[NSSet alloc] initWithArray:hkTypes];
}

每次您第一次请求新类型时,都会显示模式权限对话框(但如果您重新提示未授予的权限,它将不会再次显示)。Apple 的指导方针是提示您可能需要的所有内容,但如果我知道有人只要求保存其中的几个,那么预先请求 12 种类型对我来说有点违反最佳实践。

于 2014-10-14T04:59:53.337 回答