与请求关联的构建器开始收集时的授权错误

时间:2019-04-26 11:34:37

标签: objective-c xcode watchkit health-kit

我正在研究Watch OS的活动跟踪器。我希望能够使用Healthkit读取心率数据。该应用程序是用Objective C编写的,对于此特定任务,那里没有太多的代码示例。

通过阅读Apple文档和观看2018年WWDC视频“锻炼的新方法”,我编写了我认为应该在Objective C中工作的代码。在我达到所谓的目标之前,一切似乎都工作正常生成器以BeginCollectionWithStartDate。完成处理程序返回错误:Error Domain = com.apple.healthkit代码= 4“未授权” UserInfo = {NSLocalizedDescription =未授权}

这令我感到惊讶,因为我可以在iOS设备上的“设置”>“隐私”>“健康”>“ myAppName”下看到许可权,看起来不错。按照Apple文档中所述和上述视频中的说明,Watch应用程序在授权过程中触发iOS设备上的提示以批准请求的Healthstore访问运行。

我已经在模拟器上以及与手机配对的真实手表上进行了尝试。

有人可以帮忙吗?我正在用这个把头发拔出来!

这是我为此编写的相关目标C代码(也许我错过了什么?)。

在手表扩展名上:

请求授权(由iOS应用处理)

// awakeWithContext

if (!appHasStarted) {

    healthStore = [HKHealthStore new];

    [healthStore requestAuthorizationToShareTypes:[self dataTypesToShare] readTypes:[self dataTypesToRead]  completion:^(BOOL success, NSError * _Nullable error) {

        if (error) {
            NSLog(@"Error:requestAuthorizationToShareTypes: %@",[error localizedDescription]);
        }

    }];
}

- (IBAction)watchStartButtonPressed {

NSLog(@"Pressed start button on watch");

if (watchStartButtonState == startbuttonstatestart) {

    dateWhenPressedStart = [NSDate date];

    configuration = [[HKWorkoutConfiguration alloc] init];

    [configuration setActivityType:HKWorkoutActivityTypeCycling];

    NSError *error;

    workoutSession = [[HKWorkoutSession alloc] initWithHealthStore:healthStore configuration:configuration error:&error];

    if (error) {

        NSLog(@"Error:unable to establish a workout session: %@",[error localizedDescription]);

        return;
    }

    workoutSession.delegate = self;

    builder = workoutSession.associatedWorkoutBuilder;

    builder.delegate = self;

    HKLiveWorkoutDataSource* dataSource = [[HKLiveWorkoutDataSource alloc] initWithHealthStore:healthStore workoutConfiguration:configuration];

    builder.dataSource = dataSource;

    [workoutSession startActivityWithDate:dateWhenPressedStart];

    [builder beginCollectionWithStartDate:dateWhenPressedStart completion:^(BOOL success, NSError * _Nullable error) {

        if (error) {
            NSLog(@"Error:beginCollectionWithStartDate: %@",[error localizedDescription]);
        }

    }];


//This is the point where I get the error message.

- (NSSet *) dataTypesToShare
{
    HKQuantityType *heartRate = [HKQuantityType      quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate];
    HKQuantityType *energy = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierActiveEnergyBurned];
    HKQuantityType *distance = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceCycling];

    return [NSSet setWithObjects:heartRate,energy,distance,nil];

}

- (NSSet *)dataTypesToRead
{
    HKQuantityType *heartRate = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate];
    HKQuantityType *energy = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierActiveEnergyBurned];
    HKQuantityType *distance = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceCycling];

    return [NSSet setWithObjects:heartRate,energy,distance,nil];
}

1 个答案:

答案 0 :(得分:0)

enter image description here

为了未来的读者。将此添加到您的 Watch Extension plist。