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

问题描述 投票:1回答:1

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

[从阅读Apple文档并观看2018年WWDC视频“锻炼的新方法”,我已经编写了我认为应该在Objective C中工作的代码。在我将其称为Builder之前,一切似乎都工作正常开始CollectionWithStartDate。完成处理程序返回错误: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];
}
objective-c xcode watchkit healthkit
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.