Advertisement
Guest User

Untitled

a guest
Dec 21st, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. Hello, we're currently trying to get HealthKit to work in the background, in order to deliver steps data to our server when the App is closed.
  2.  
  3. For experimental purposes we've created a brand new iOS project in XCode, enabled HealhtKit and all background modes in Compabilities. After that, we pretty much run the code (see further down).
  4.  
  5. So what happens first is that the app ofcourse asks for the permissions, which we grant. Then it sends the data to the server. What we're expecting is that the app should keep deliver the steps data every hour, to the server. But it doesnt do that, it seems like the app cant do anything when it's not active.
  6.  
  7. [code]
  8. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  9. [self setTypes];
  10. return YES;
  11. }
  12.  
  13.  
  14. -(void) setTypes
  15. {
  16. self.healthStore = [[HKHealthStore alloc] init];
  17.  
  18. NSMutableSet* types = [[NSMutableSet alloc]init];
  19. [types addObject:[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]];
  20.  
  21. [self.healthStore requestAuthorizationToShareTypes: types
  22. readTypes: types
  23. completion:^(BOOL success, NSError *error) {
  24.  
  25. dispatch_async(dispatch_get_main_queue(), ^{
  26. [self observeQuantityType];
  27. [self enableBackgroundDeliveryForQuantityType];
  28. });
  29. }];
  30. }
  31.  
  32. -(void)enableBackgroundDeliveryForQuantityType{
  33. [self.healthStore enableBackgroundDeliveryForType: [HKQuantityType quantityTypeForIdentifier: HKQuantityTypeIdentifierStepCount] frequency:HKUpdateFrequencyImmediate withCompletion:^(BOOL success, NSError *error) {
  34. }];
  35. }
  36.  
  37.  
  38. -(void) observeQuantityType{
  39.  
  40. HKSampleType *quantityType = [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
  41.  
  42. HKObserverQuery *query =
  43. [[HKObserverQuery alloc]
  44. initWithSampleType:quantityType
  45. predicate:nil
  46. updateHandler:^(HKObserverQuery *query,
  47. HKObserverQueryCompletionHandler completionHandler,
  48. NSError *error) {
  49.  
  50. dispatch_async(dispatch_get_main_queue(), ^{
  51. if (completionHandler) completionHandler();
  52. [self getQuantityResult];
  53.  
  54. });
  55. }];
  56. [self.healthStore executeQuery:query];
  57. }
  58.  
  59.  
  60. -(void) getQuantityResult{
  61.  
  62. NSInteger limit = 0;
  63. NSPredicate* predicate = nil;
  64.  
  65. NSString *endKey = HKSampleSortIdentifierEndDate;
  66. NSSortDescriptor *endDate = [NSSortDescriptor sortDescriptorWithKey: endKey ascending: NO];
  67.  
  68. HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType: [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]
  69. predicate: predicate
  70. limit: limit
  71. sortDescriptors: @[endDate]
  72. resultsHandler:^(HKSampleQuery *query, NSArray* results, NSError *error){
  73.  
  74. dispatch_async(dispatch_get_main_queue(), ^{
  75. // sends the data using HTTP
  76. [self sendData: [self resultAsNumber:results]];
  77.  
  78. });
  79. }];
  80. [self.healthStore executeQuery:query];
  81. }
  82. [/code]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement