I am working an application in which I am fetching steps from Apple Health application. I need to update these steps on my server when the application is terminated or in the background. I implemented silent push notification and it's working fine. I am getting sound when notification is received(for testing purpose) when the application is terminated or in the background. When the application is in background steps are updating on the server but if the application is terminated only sound plays but steps not updating on the server.
NSString *leastJoinedGameDate = [[NSUserDefaults standardUserDefaults] valueForKey:kLeastJoinedDate];
[[GSHealthKitManager sharedManager] fetchStepsHistoryWithStartDate:leastJoinedGameDate andCompletionHandler:^(NSMutableArray *results, NSError *error) {
NSLog(@"%@",results);
if (error) {
if (_completionHandler) {
_completionHandler(UIBackgroundFetchResultFailed);
}
}
else{
[self uploadStepsHistoryToServerWithStepsHistory:results];
}
NSLog(@"%@",error);
}];
-(void)uploadStepsHistoryToServerWithStepsHistory:(NSArray *)stepsArray{
if (![AppHelper isNetworkAvailableWithoutAlert]) {
return;
}
NSMutableDictionary *requestDict = [NSMutableDictionary new];
requestDict[kOption] = @"update_steps";
requestDict[kUserID] = [[UserDetails sharedInstance] userID];
requestDict[@"progress"] = stepsArray;
[WebServicesHandler performPOSTRequestWithURL:kServerURL andParameters:requestDict andAcessToken:[[UserDetails sharedInstance] accessToken] completion:^(id result, NSError *error) {
if (_completionHandler) {
_completionHandler(UIBackgroundFetchResultNoData);
}
NSString *currentDateStr = [AppHelper getCurrentDateInStringWithoutTime];
[[NSUserDefaults standardUserDefaults] setObject:currentDateStr forKey:kPreviousDayDate];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"%@",result);
}];
}