I am attempting to do a quick POST via NSURLSession at application termination. Application termination is defined as the user swipes it out when on the home screen. Furthermore, applicationWillTerminate is demonstratively called (so that's not in question).
My intent is to inform the server that this user is now going offline.
This, however, does not work .. as I don't receive a POST on my backend. I am trying to figure out why this doesn't work.. or even if it should? According to Apple docs, this should work if it can execute within 5 seconds.. although I don't even have a completion handler so it should be executed. Any help or advice will be greatly appreciated.
My implementation is as follows (for reference).
- (void)applicationWillTerminate:(UIApplication *)application {
NSMutableDictionary * gatherAllInputs = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
@"blah@df.com, @"emailEntry",
@"False", @"isPersonAvailable",
nil];
NSError *error;
NSData *gatherAllInputsJSON = [NSJSONSerialization dataWithJSONObject:gatherAllInputs options:0 error:&error];
NSString* theDataSentToServer;
theDataSentToServer = [[NSString alloc] initWithData:gatherAllInputsJSON encoding:NSUTF8StringEncoding];
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *postingSession = [NSURLSession sessionWithConfiguration:sessionConfiguration];
NSURL *url = [NSURL URLWithString:@"https://blahblahblah.com/someblah"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[theDataSentToServer dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionDataTask *postSomething = [postingSession dataTaskWithRequest:request];
[postSomething resume];
NSLog(@"Got here!"); //<-- THIS GETS OUTPUTTED.. SO I KNOW THIS CODE IS BEING CALLED!
}
EDIT:
Similarly, We've messaging app with XMPP framework. Require to terminate/Disconnect the XMPP connection on applicationWIllTerminate delegate. Indeed, applicationWIllTerminate will be called when an app is in foreground and user switched to multitasking UI(double tap home button). tried by calling XMPP Disconnection but no luck.
Can't use WillResignActive for disconnection, Where in DidEnteredBackground delegate is not allowed this case for any time extension.
Server-client Ping pong acts every 2 minutes so cont relay on that as well.
Looking out a feasible solution to disconnect XMPP at user force quits an app.