根据以下来自Fit应用程序的Apple代码,如果success
为NO
,那么我们确定用户具有对应用程序的访问权限以读取或写入数据,但此方法在iOS9中已更改在iOS9中,我们有“不允许”和“允许”按钮,无论用户做出什么选择,它都会将success
作为YES
返回意味着我们无法再确定用户是否允许。
在我的应用中,我想向用户显示步骤以激活Health工具包,我依赖于此success
标志,而在iOS9中则失败了。根据Apple doc(下面引用),他们说如果没有权限,它将显示为没有所请求类型的数据,现在我无法确定它是否因为没有数据或因为用户没有提供访问权限
另请在上面找到Apple的开发链接 Developer forums
想知道是否有正确的方法。
我想到的一个解决方法是在回调后检查是否有有效的来源,对我来说,我正在寻找设备作为源,如果它不可用我认为这是一个auth问题,我正在推杆那个代码也在这里,我可能会使用它,如果没有找到任何其他选项
为了帮助防止敏感健康信息泄露,您的 应用无法确定用户是否已授予阅读权限 数据。如果您未获得许可,就会显示出来 没有HealthKit商店中所请求类型的数据。如果您的应用程序 获得共享权限但未获得读取权限,您只能看到 您的应用已写入商店的数据。来自其他来源的数据 仍然隐藏着。
Fit App中的代码
if ([HKHealthStore isHealthDataAvailable]) {
NSSet *writeDataTypes = [self dataTypesToWrite];
NSSet *readDataTypes = [self dataTypesToRead];
[self.healthStore requestAuthorizationToShareTypes:writeDataTypes readTypes:readDataTypes completion:^(BOOL success, NSError *error) {
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.
[self updateUsersAgeLabel];
[self updateUsersHeightLabel];
[self updateUsersWeightLabel];
});
}];
}
消息来源检测
- (void)getStepSourceWithCompletion:(CompletionHandler)completion {
NSString *deviceName = [[UIDevice currentDevice] name];
HKSampleType *sampleType = [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
HKSourceQuery *sourceQuery = [[HKSourceQuery alloc] initWithSampleType:sampleType
samplePredicate:nil
completionHandler:^(HKSourceQuery * _Nonnull query, NSSet<HKSource *> * _Nullable sources, NSError * _Nullable error) {
if (error) {
return completion(nil, error);
}
if (sources && sources.count > 0) {
for (HKSource *source in sources) {
// Check if device is a source.
if ([source.name isEqual:deviceName]) {
return completion(source, nil);
break;
}
}
}
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
[userInfo setValue:@"No suitable sources were found" forKey:NSLocalizedDescriptionKey];
NSError *noSource = [NSError errorWithDomain:domain code:ErrorCodeNoSource userInfo:userInfo];
completion(nil, noSource);
}];
[self.healthStore executeQuery:sourceQuery];
}
答案 0 :(得分:0)
授权回调中的success
值从未表明用户是允许还是拒绝您的应用授权。在iOS 9之前,如果用户取消授权表,success
将为NO,这意味着用户拒绝做出决定。但是,从iOS 9开始,用户无法延迟决定是否授权您的应用。
你不应该试图解决这种缺乏信息的问题。 HealthKit有意保护用户的隐私。