Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

willTerminate Webservice Call in ios

Tags:

ios

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..

like image 552
Jiten Parmar Avatar asked Apr 08 '14 16:04

Jiten Parmar


2 Answers

There is not 100% sure way to achieve this, but you can use this.

  1. You can use location update method, to achieve this task. if location permission is always authorized, it continues calls didUpdateLocation Method even after application closed.

I have used this method in one of my application, and its working perfect.

  1. 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.

  1. 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.

like image 162
g212gs Avatar answered Oct 23 '22 10:10

g212gs


   - (void)applicationWillTerminate:(UIApplication *)application {
      [comm getDataFrom:offlineURL forHttpMethod:@"POST" withHttpBody:httpBody 
      forRequest:2];
       sleep(3);
   }
like image 1
Ravindra Avatar answered Oct 23 '22 12:10

Ravindra