I am developing an application in iPhone & iPad & i am calling webservice (PHP) in background and sending location updates, that i have done but i also want to call a webservice when i remove the app from the background, so is that can be possible to make the app stay for few time so that i can call the webservice, i have tried with willterminate delegate method
What i mean as the term of background is to call a webservice when i terminate the app
this is what I am trying to call a method from the notificationcenter, it will call a webservice from different controller.
- (void)applicationWillTerminate:(UIApplication *)application
{
[[NSNotificationCenter defaultCenter]postNotificationName:@"Deactiviate" object:nil];
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
but when i terminate the app webservice is not called.
Any suggestion, guesses Thanx in advance..
There is not 100% sure way to achieve this, but you can use this.
I have used this method in one of my application, and its working perfect.
If you are using NSURLSession, you can use following techniques.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//Sample webservice block
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];
downloadTask = [session downloadTaskWithURL:[NSURL URLWithString:@"http://cdn.tutsplus.com/mobile/uploads/2013/12/sample.jpg"]]; // downloadTask = NSURLSessionDownloadTask object
return YES;
}
Now in ApplicationWillTerminate:
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
[downloadTask resume];
sleep(5); // sleep main thread for 5 seconds after webservice call, so that it gets time to call webservice.
}
we can also use GCD to call "resume" call at main thread.
Note: Webservice should be as light as possible so that we can get more possibility for making successful web service call.
I have checked in one of my application to make the user offline while user quit the application.
By using runLoopMode in Swift 3.0:
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
var finished = false
self.webServiceBlock { success in
print("response we got here")
finished = true
}
while !finished {
RunLoop.current.run(mode: RunLoopMode.defaultRunLoopMode,
before: NSDate.distantFuture)
}
}
I have checked both this technique in two different application, and both time I got success, but still there are some possibilities where I can say this technique, not 100% perfect.
- (void)applicationWillTerminate:(UIApplication *)application {
[comm getDataFrom:offlineURL forHttpMethod:@"POST" withHttpBody:httpBody
forRequest:2];
sleep(3);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With