HealthKit(iOS)不会在后台传递数据(objC)

前端之家收集整理的这篇文章主要介绍了HealthKit(iOS)不会在后台传递数据(objC)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我们正在尝试让HealthKit在后台工作,以便在应用程序关闭时将步骤数据提供给我们的服务器.

为了实验目的,我们在XCode中创建了一个全新的iOS项目,启用了HealhtKit和Compabilities中的所有后台模式.之后,我们几乎运行代码(见下文).

那么首先发生的是应用程序要求我们授予的权限.我们期望的是应用程序应该每小时将数据传递到服务器.但它没有这样做,似乎应用程序不能做任何事情,当它不活跃.

该应用程序仅在恢复或启动时才提供数据,但根本不提供背景信息(软关闭/硬关闭)

appdelegate.m:

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  2. [self setTypes];
  3. return YES;
  4. }
  5.  
  6.  
  7. -(void) setTypes
  8. {
  9. self.healthStore = [[HKHealthStore alloc] init];
  10.  
  11. NSMutableSet* types = [[NSMutableSet alloc]init];
  12. [types addObject:[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]];
  13.  
  14. [self.healthStore requestAuthorizationToShareTypes: types
  15. readTypes: types
  16. completion:^(BOOL success,NSError *error) {
  17.  
  18. dispatch_async(dispatch_get_main_queue(),^{
  19. [self observeQuantityType];
  20. [self enableBackgroundDeliveryForQuantityType];
  21. });
  22. }];
  23. }
  24.  
  25. -(void)enableBackgroundDeliveryForQuantityType{
  26. [self.healthStore enableBackgroundDeliveryForType: [HKQuantityType quantityTypeForIdentifier: HKQuantityTypeIdentifierStepCount] frequency:HKUpdateFrequencyImmediate withCompletion:^(BOOL success,NSError *error) {
  27. }];
  28. }
  29.  
  30.  
  31. -(void) observeQuantityType{
  32.  
  33. HKSampleType *quantityType = [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
  34.  
  35. HKObserverQuery *query =
  36. [[HKObserverQuery alloc]
  37. initWithSampleType:quantityType
  38. predicate:nil
  39. updateHandler:^(HKObserverQuery *query,HKObserverQueryCompletionHandler completionHandler,NSError *error) {
  40.  
  41. dispatch_async(dispatch_get_main_queue(),^{
  42. if (completionHandler) completionHandler();
  43. [self getQuantityResult];
  44.  
  45. });
  46. }];
  47. [self.healthStore executeQuery:query];
  48. }
  49.  
  50.  
  51. -(void) getQuantityResult{
  52.  
  53. NSInteger limit = 0;
  54. NSPredicate* predicate = nil;
  55.  
  56. NSString *endKey = HKSampleSortIdentifierEndDate;
  57. NSSortDescriptor *endDate = [NSSortDescriptor sortDescriptorWithKey: endKey ascending: NO];
  58.  
  59. HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType: [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]
  60. predicate: predicate
  61. limit: limit
  62. sortDescriptors: @[endDate]
  63. resultsHandler:^(HKSampleQuery *query,NSArray* results,NSError *error){
  64.  
  65. dispatch_async(dispatch_get_main_queue(),^{
  66. // sends the data using HTTP
  67. [self sendData: [self resultAsNumber:results]];
  68.  
  69. });
  70. }];
  71. [self.healthStore executeQuery:query];
  72. }

解决方法

我看到可能在AppDelegate中引起问题的内容,特别是这一行:
  1. [[NSURLConnection alloc] initWithRequest:request delegate:self];

这是创建一个NSURLConnection,但不启动它.尝试改为:

  1. NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
  2. [connection start];

编辑:再次查看文档后

他们建议在您的应用程序中设置观察器查询didFinishLaunchingWithOptions:方法.在上面的代码中,将HKObserverQuery设置在授权处理程序中,该处理程序在随机后台队列中调用.尝试进行此更改以在主线程上进行设置:

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  2. [self setTypes];
  3. [self observeQuantityType];
  4. return YES;
  5. }

HKObserverQuery Reference

猜你在找的iOS相关文章