Gur*_*ngh 5 iphone objective-c ios healthkit
我有一个应用程序,我正在尝试集成HealthKit并使用HKStatisticsCollectionQuery提取白天汇总的步骤相关数据.要求是单独提取特定于iPhone和Apple Watch设备的步骤数据(无重复数据删除),这些数据对健康应用程序有所贡献.
该HKSource类仅包含以下属性:
我能够提取所有源码(使用HKSourceQuery),其中bundleIdentifier前缀为"com.apple.health",但我无法推断出哪个是Apple iPhone,哪个是Apple iWatch.
以前是否有人遇到类似的情况,有没有其他方法来确定哪个来源是iPhone或Apple Watch?
任何帮助都会很棒!谢谢!
不是最好的解决方案,但是,我已经找到了一种方法来区分手表和手机使用以下过程:
我注意到来自iPhone/Watch的所有步骤数据都具有以下bundleIdentifier格式:
com.apple.health.DeviceUUID
请注意,在Health应用程序中手动输入的数据的包标识符为com.apple.Health(大写为"H").
所以,首先,使用以下方法获取手机的设备名称:
NSString *deviceName = [[UIDevice currentDevice] name];
Run Code Online (Sandbox Code Playgroud)
接下来,获取bundleIdentifier中前缀匹配为'com.apple.health'的所有源.这应该为您提供iPhone和Apple手表作为有效来源,并忽略手动条目和所有其他应用程序.
接下来,检查设备的名称是否与源中的相同,然后是您的iPhone,另一个来源应该是Apple Watch.
以下是获取源的示例源查询:
- (void)fetchSources
{
NSString *deviceName = [[UIDevice currentDevice] name];
NSMutableArray *dataSources = [[NSMutableArray alloc] init];
HKQuantityType *stepsCount = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
HKSourceQuery *sourceQuery = [[HKSourceQuery alloc] initWithSampleType:stepsCount
samplePredicate:nil
completionHandler:^(HKSourceQuery *query, NSSet *sources, NSError *error)
{
for (HKSource *source in sources)
{
if ([source.bundleIdentifier hasPrefix:sourceIdentifier])
{
if ([source.name isEqualToString:deviceName])
// Iphone
else
// Apple Watch
[dataSources addObject:source];
}
}
}];
[self.healthStore executeQuery:sourceQuery];
}
Run Code Online (Sandbox Code Playgroud)
您现在可以使用NSPredicate类为每个数据源创建一个谓词:
NSPredicate *sourcesPredicate = [HKQuery predicateForObjectsFromSource:source];
Run Code Online (Sandbox Code Playgroud)
请注意,我的第一个想法是匹配UUID,但是当我使用NSUUID类生成UUID时,它与拉出源中的包标识符中存在的UUID不匹配.
此外,您可以将手机名称更改为您想要的任何名称,它也会在Health应用程序中自动更新.
正如我所说,不是最好的解决方案,但对我有用,这是我能找到的唯一方法.如果您能找到更好的解决方案,请告诉我.谢谢.
| 归档时间: |
|
| 查看次数: |
2731 次 |
| 最近记录: |