即使Apple手表屏幕关闭,也可以访问锻炼数据

时间:2015-08-30 19:19:42

标签: objective-c watchkit apple-watch health-kit

我成功在没有锻炼会话的情况下在apple watch os 2上获得实时心率数据。但是当Apple Watch屏幕关闭时,我的完成块不再被调用。我想继续在现场管理这些数据,并在心率过低时让我的手机响铃。 也许我可以让iphone perma上的应用程序打开,也许它可以在锻炼期间访问healthkit数据? 你认为这可行吗?或者你有其他想法吗?

此致

2 个答案:

答案 0 :(得分:3)

嘿,我找到了解决方案:

我将iphone应用程序放在前台:

[UIApplication sharedApplication].idleTimerDisabled = YES

使用与apple watch(HKAnchoredObjectQuery)相同的查询,我可以访问最新的健康工具包数据。即使我的苹果手表关闭(锻炼时间),我也能获得实时心率数据

  • 我的查询

HKQuantityType * type = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate];

HKAnchoredObjectQuery *heartRateQuery = [[HKAnchoredObjectQuery alloc]
                                     initWithType:type
                                     predicate:nil
                                     anchor:self.anchor
                                     limit:HKObjectQueryNoLimit
                                     resultsHandler:^(HKAnchoredObjectQuery * _Nonnull query, NSArray<__kindof HKSample *> * _Nullable sampleObjects, NSArray<HKDeletedObject *> * _Nullable deletedObjects, HKQueryAnchor * _Nullable newAnchor, NSError * _Nullable error) {
                                         if (error) {

                                             // Perform proper error handling here...
                                             NSLog(@"*** An error occured while performing the anchored object query. %@ ***",
                                                   error.localizedDescription);

                                         }

                                         self.anchor = newAnchor;

                                         HKQuantitySample *sample = (HKQuantitySample *)[sampleObjects firstObject];
                                         if (sample) {
                                             double value = [sample.quantity doubleValueForUnit:[HKUnit unitFromString:@"count/min"]];

                                             dispatch_async(dispatch_get_main_queue(), ^(void){
                                                 self.heartrateLabel.text = [NSString stringWithFormat:@"%0.0f",value];
                                             });
                                             NSLog([NSString stringWithFormat:@"%0.0f",value]);
                                             [self.hkStore stopQuery:heartRateQuery];


                                         }
                                     }];

[self.hkStore executeQuery:heartRateQuery];

答案 1 :(得分:0)

按照设计,当手表屏幕关闭时,不允许使用watchOS 2应用程序。你无法改变这种行为。