xcode 请求关联生成器开始收集时出现授权错误

rbl8hiat  于 2023-02-16  发布在  其他
关注(0)|答案(2)|浏览(126)

我正在为手表操作系统的活动跟踪器工作。我希望能够使用Healthkit读取心率数据。该应用程序是用Objective C编写的,对于这个特定的任务,没有那么多的代码示例。
通过阅读Apple文档和观看2018年WWDC视频“New Ways to Work With Workouts”,我编写了我认为应该在Objective C中工作的代码。一切似乎都工作正常,直到我调用builder来beginCollectionWithStartDate。错误域=com.apple.healthkit代码=4“未授权”用户信息={NSLocalizedDescription=未授权}
这让我很惊讶,因为我可以看到iOS设备上的设置〉隐私〉健康〉myAppName下的权限看起来不错。授权过程,即手表应用程序触发iOS设备上的提示,以批准所请求的健康商店访问运行在苹果的文档中所描述的和前面提到的视频所示。
我在模拟器上和一个真实的的手表上都试过了。
有谁能帮帮忙吗?我都快被这家伙扯毛了!
下面是我编写的相关Objective 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];
}
ddrv8njm

ddrv8njm1#

对于未来的读者。添加到您的手表扩展plist。

fwzugrvs

fwzugrvs2#

对于未来的“未来”读者。我做了Matin De Simone做的事情,但最终解决我的问题的是,我在IOS设备上的健康应用程序中切换了隐私设置,以允许我的WatchOS应用程序读取健康数据。

相关问题