我正在使用Apple Health应用程序从中获取步骤的应用程序。当应用程序终止或在后台时,我需要在我的服务器上更新这些步骤。我实施了静音推送通知,它工作正常。当应用程序终止或在后台收到通知(用于测试目的)时,我会收到声音。当应用程序处于后台步骤时,正在服务器上进行更新,但如果应用程序终止,则仅播放声音但步骤未在服务器上更新。
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);
}];
}