HKObserverQuery 有以下方法支持在后台接收更新:
- initWithSampleType:predicate:updateHandler:
Run Code Online (Sandbox Code Playgroud)
该updateHandler有一个completionHandler具有以下文件:
该块传递给更新处理程序.完成处理传入数据后,必须立即调用此块.调用此块告诉HealthKit您已成功接收到后台数据.如果您不调用此块,HealthKit将继续尝试使用退避算法启动您的应用.如果您的应用无法响应三次,HealthKit会假定您的应用无法接收数据,并停止向您发送后台更新.
通过查看其他帖子,似乎围绕此处理程序存在很多混乱.以下是我对它的一些问题:
CoreBluetooth在后台使用时,它只会在后台唤醒您的应用程序10秒钟.无需调用任何处理程序或处理后台更新3次攻击.我有一个通用的应用程序,我已经实现它以使用healthkit API.它在iPhone上运行良好,但当我尝试将其安装在实际的iPad设备上(在iPad模拟器中完美运行)时,它不会安装.
我收到错误消息:"应用程序的Info.plist在...指定设备功能要求,"我的ipad""无法满足.
我有什么方法可以分发一个通用应用程序,其中healthkit API(框架)只在iPhone上运行?
下面是我项目的两个截图:


我真的需要制作一个独立的iPad应用吗?
如果没有运行,HealthKit后台交付可以启动应用程序吗?特别是在终止状态?
我正在制作一个快速的iOS应用程序,它集成了Health应用程序报告的用户步数.我可以在最后一小时轻松找到用户的步数,使用它作为我的谓词:
let anHourBeforeNow: NSDate = NSDate().dateByAddingTimeInterval(-60 * 60)
let predicate = HKQuery.predicateForSamplesWithStartDate(anHourBeforeNow, endDate: NSDate(), options: .None)
Run Code Online (Sandbox Code Playgroud)
我休息了,所以我可以成功访问用户在最后一小时的步数.但是,如何从一天开始就访问用户的步骤数据,例如步骤部分中显示的Health应用程序?
我想做这样的事情:
let date = NSDate()
let cal = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
let newDate = cal.startOfDayForDate(date)
let predicate = HKQuery.predicateForSamplesWithStartDate(newDate, endDate: NSDate(), options: .None)
Run Code Online (Sandbox Code Playgroud)
但是这段代码没有针对时区进行调整(所以它给了我UTC当天的开始,而不是用户所在的那天的开始),而且我也得到了高度膨胀的步数(原因未知).
我有一个使用HealthKit和Swift的应用程序,当我在模拟器或设备上从Xcode测试时,所有内容都编译并运行良好.我测试,调试它,并将其提交到iTunes商店.当应用程序获得批准后,我从商店下载了新版本,它立即崩溃了.这非常令人费解,因为我之前从未遇到过这个问题.我查看手机上的诊断和使用日志,应用程序的崩溃报告说:
Dyld错误消息:未加载库:@ rpath/libswiftCore.dylib引用自:/ path/to/my/app原因:找不到合适的映像.确实发现:/path/to/my/app/Frameworks/libswiftCore.dylib:在地址MMAP()错误1 = 0x100174000,大小= 0x0194000段= __ TEXT在段::地图()
有没有人见过这样的东西或知道如何处理它?
谢谢
编辑:
这个崩溃的问题在10月4日早上神奇地解决了,这告诉我这是一个苹果问题.我一直在通过电子邮件发送iTunes Connect支持,但他们没有说出这种或那种方式.我还开了一张技术票,甚至没有回复Apple的回复.这非常令人失望,不仅因为苹果公司对这个问题没有透明度,而且这让我觉得如果再次发生这种情况,我后来就无法采取行动.
我现在在尝试上传到应用商店时遇到此错误,但我没有对我的 healthkit 功能进行任何更改,而且一切看起来都很好(参见图片)。为什么说价值是[] 
<
?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>aps-environment</key>
<string>development</string>
<key>com.apple.developer.healthkit</key>
<true/>
<key>com.apple.developer.healthkit.access</key>
<array/>
<key>com.apple.developer.icloud-container-identifiers</key>
<array>
<string>iCloud.codes.myndarc.newFitnessApp</string>
</array>
<key>com.apple.developer.icloud-services</key>
<array>
<string>CloudKit</string>
</array>
<key>com.apple.security.application-groups</key>
<array>
<string>group.com.myndarc.newFitnessApp</string>
</array>
</dict>
</plist>
Run Code Online (Sandbox Code Playgroud)
我正在使用HealthKit来读取某些类型的信息.我特别不要求写功能.当尝试检测用户是否允许读取某个健康类型时,会出现此问题.
我相信这样做的目的是使用HKHealthStore的authorizationStatusForType方法,但这只是被拒绝或未知.它只返回授权写入类型.有没有人找到一种方法来使用这种方法阅读或另一种解决方法?
HKQuantityType *stepsType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight];
HKAuthorizationStatus status = [self.healthStore authorizationStatusForType:stepsType];
Run Code Online (Sandbox Code Playgroud) 我想检查HeathKit是否已被授权我读取用户的数据,如果我被授权进行训练,如果没有弹出警报.但requestAuthorizationToShareTypes似乎总是返回true?如何获得用户是否授权我的参考?
override func viewDidLoad() {
super.viewDidLoad()
//1. Set the types you want to read from HK Store
let healthKitTypesToRead: [AnyObject?] = [
HKObjectType.workoutType()
]
//2. If the store is not available (for instance, iPad) return an error and don't go on.
if !HKHealthStore.isHealthDataAvailable() {
let error = NSError(domain: "com.myndarc.myrunz", code: 2, userInfo: [NSLocalizedDescriptionKey: "HealthKit is not available in this Device"])
print(error)
let alertController = UIAlertController(title: "HealthKit Not Available", message: "It doesn't look like HealthKit is available on your device.", preferredStyle: …Run Code Online (Sandbox Code Playgroud) 我们目前正在尝试让HealthKit在后台运行,以便在App关闭时向我们的服务器提供步骤数据.
出于实验目的,我们在XCode中创建了一个全新的iOS项目,启用了HealhtKit以及Compabilities中的所有后台模式.在那之后,我们几乎运行代码(见下文).
所以首先发生的是应用程序请求权限,我们授予.我们期待的是应用程序应该每小时向服务器提供步骤数据.但它没有这样做,似乎应用程序不能做任何事情,当它不活跃.
该应用程序仅在恢复或启动时提供数据,但在背景中根本不提供(软关闭/硬关闭)
appdelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self setTypes];
return YES;
}
-(void) setTypes
{
self.healthStore = [[HKHealthStore alloc] init];
NSMutableSet* types = [[NSMutableSet alloc]init];
[types addObject:[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]];
[self.healthStore requestAuthorizationToShareTypes: types
readTypes: types
completion:^(BOOL success, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
[self observeQuantityType];
[self enableBackgroundDeliveryForQuantityType];
});
}];
}
-(void)enableBackgroundDeliveryForQuantityType{
[self.healthStore enableBackgroundDeliveryForType: [HKQuantityType quantityTypeForIdentifier: HKQuantityTypeIdentifierStepCount] frequency:HKUpdateFrequencyImmediate withCompletion:^(BOOL success, NSError *error) {
}];
}
-(void) observeQuantityType{
HKSampleType *quantityType = [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
HKObserverQuery *query =
[[HKObserverQuery alloc]
initWithSampleType:quantityType
predicate:nil
updateHandler:^(HKObserverQuery …Run Code Online (Sandbox Code Playgroud) 我不能在我的应用程序中显示任何卡路里/ activeEnergyBurned,并且不知道为什么?
WorkoutInterfaceController:
private func totalCalories() -> Double {
return totalEnergyBurned.doubleValue(for: HKUnit.kilocalorie())
}
private func setTotalCalories(calories: Double) {
totalEnergyBurned = HKQuantity(unit: HKUnit.kilocalorie(), doubleValue: calories)
}
func startQuery(quantityTypeIdentifier: HKQuantityTypeIdentifier) {
let datePredicate = HKQuery.predicateForSamples(withStart: workoutStartDate, end: nil, options: .strictStartDate)
let devicePredicate = HKQuery.predicateForObjects(from: [HKDevice.local()])
let queryPredicate = NSCompoundPredicate(andPredicateWithSubpredicates:[datePredicate, devicePredicate])
let updateHandler: ((HKAnchoredObjectQuery, [HKSample]?, [HKDeletedObject]?, HKQueryAnchor?, Error?) -> Void) = { query, samples, deletedObjects, queryAnchor, error in
self.process(samples: samples, quantityTypeIdentifier: quantityTypeIdentifier)
}
let query = HKAnchoredObjectQuery(type: HKObjectType.quantityType(forIdentifier: quantityTypeIdentifier)!,
predicate: queryPredicate,
anchor: …Run Code Online (Sandbox Code Playgroud)