屏幕locking时HealthStore enableBackgroundDelivery

您好,我正在尝试安装启用后台交付的健康商店观察员。 我的问题是,屏幕locking时,它不会传递任何东西。 我已经简化了我的代码这个问题的重点:)我有我的plist HealthKit,我已经接受healthStoretypes的步数。 当应用程序打开并且屏幕未locking时,一切正常。 但是,当屏幕locking,我没有得到任何意见。 为了testing目的,频率被设置为立即。

我的代码如下

- (void)setupHealthStore{ if ([HKHealthStore isHealthDataAvailable]) { NSSet *readDataTypes = [self dataTypesToRead]; self.healthStore = [[HKHealthStore alloc]init]; [self.healthStore requestAuthorizationToShareTypes:nil readTypes:readDataTypes completion:^(BOOL success, NSError *error) { if (success) { HKQuantityType *quantityType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]; [self.healthStore enableBackgroundDeliveryForType:quantityType frequency:HKUpdateFrequencyImmediate withCompletion:^(BOOL success, NSError *error) { if (success) { [self setupObserver]; } }]; } }]; } 

}

上述方法在AppDelegate didfinishLaunchWithOptions中调用

下一个方法是

 - (void)setupObserver{ HKQuantityType *quantityType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]; HKObserverQuery *query = [[HKObserverQuery alloc]initWithSampleType:quantityType predicate:nil updateHandler:^(HKObserverQuery *query, HKObserverQueryCompletionHandler completionHandler, NSError *error) { if (!error) { [self alarm]; if (completionHandler) { NSLog(@"Completed"); completionHandler(); } } else { if (completionHandler) { completionHandler(); } } }]; [self.healthStore executeQuery:query]; 

}

当我打开应用程序,它立即返回观察。

当iPhone被locking时,您无法以任何方式访问healthKit数据。

当iPhone被解锁,但应用程序在后台,您只能使用HKObserverQuery,这是用来知道是否添加一些新的样品。

当iPhone解锁并且应用处于前台时,您可以使用与HealthKit Framework相关的所有内容。

我能够得到这个工作观察体重和血糖变化HealthKit。

ApplicationDelegate中

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch. GlobalHealthManager.startObservingWeightChanges() return true } 

HealthManager.swift

  let past = NSDate.distantPast() as NSDate let now = NSDate() return HKQuery.predicateForSamplesWithStartDate(past, endDate:now, options: .None) }() //here is my query: lazy var query: HKObserverQuery = {[weak self] in let strongSelf = self! return HKObserverQuery(sampleType: strongSelf.weightQuantityType, //predicate: strongSelf.longRunningPredicate, predicate : nil, //all samples delivered updateHandler: strongSelf.weightChangedHandler) }() func startObservingWeightChanges(){ healthKitStore?.executeQuery(query) healthKitStore?.enableBackgroundDeliveryForType(weightQuantityType, frequency: .Immediate, withCompletion: {(succeeded: Bool, error: NSError!) in if succeeded{ println("Enabled background delivery of weight changes") } else { if let theError = error{ print("Failed to enable background delivery of weight changes. ") println("Error = \(theError)") } } }) } /** this should get called in the background */ func weightChangedHandler(query: HKObserverQuery!, completionHandler: HKObserverQueryCompletionHandler!, error: NSError!){ NSLog(" Got an update Here ") /** this function will get called each time a new weight sample is added to healthKit. //Here, I need to actually query for the changed values.. //using the standard query functions in HealthKit.. //Tell IOS we're done... updated my server, etc. completionHandler() } } 

步骤的最低更新频率为1小时,这意味着您的应用每小时只能唤醒一次。 一旦你打开应用程序,你的观察者查询立即被解雇。 请参阅关于enableBackgroundDeliveryForType的讨论中的说明。

https://developer.apple.com/library/prerelease/ios/documentation/HealthKit/Reference/HKHealthStore_Class/index.html#//apple_ref/occ/instm/HKHealthStore/enableBackgroundDeliveryForType:frequency:withCompletion

在HKObserverQuery的情况下,您必须启用应用程序的任何后台模式才能在后台(应用程序未终止)通知ObserverQuery。

在这里输入图像说明