0

Suppose I call a webservice when the app is in foreground. Now if the user sends the app to background then how do I make sure that this webservice call keeps executing in the background.

This is the piece of code that I am using in my app.

Login* login = [[Login alloc]init];
[login initiateSignInProcess];

initiateSignInProcess has 4 web service calls. they are normal functions. I am using AFNetworking.

If any of the services fail, I call it again with a delay in the failure block of afnetworking code like below:-

failure:^(AFHTTPRequestOperation *operation, NSError *error)
         {
[self performSelector:@selector(getUserId)  withObject:nil afterDelay:5];
}

Now I want to know that if the user sends the app to background, then how will the code execute? Will it call this function in bakcground till it succeeds?

pkc456
  • 8,350
  • 38
  • 53
  • 109
Akshara
  • 141
  • 1
  • 11

4 Answers4

5

Best to use Background Process for fetch. Here is great tutorial for solution [ http://code.tutsplus.com/tutorials/ios-7-sdk-working-with-background-fetch--mobile-20520

PuNeet Pal
  • 59
  • 7
  • I tried the example and also tried sending a push notification. Everything worked as required. Thank you so much. But the nsurlsession is not executing. it is neither going to the success block nor the failure block. How do I download data from server? Kindly help – Akshara Oct 16 '15 at 09:36
1

Not possible in iOS6.x or lesser unless your application is has specific requirement to run in background like locations, Voip, music etc...

However this is possible with iOS7, please consider having a look at this

http://redth.codes/ios7-recipe-background-fetching/

Hitesh Boricha
  • 114
  • 1
  • 11
  • I tried the example and also tried sending a push notification. Everything worked as required. Thank you so much. But the nsurlsession is not executing. it is neither going to the success block nor the failure block. How do I download data from server? Kindly help – Akshara Oct 16 '15 at 06:45
1
    **For(large FIle Downloade use Asynchronous Method)** 

       NSURL *myUrl = [NSURL URLWithString:@"Enter URL HERE"];
       NSURLRequest *myRequest = [NSURLRequest requestWithURL:myUrl cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
       NSMutableData *myData = [[NSMutableData alloc] initWithLength:0];
       NSURLConnection *myConnection = [[NSURLConnection alloc] initWithRequest:myRequest delegate:self startImmediately:YES];

    **For(Small FIle Downloade use Synchronous Method)**
       NSURL *myUrl = [NSURL URLWithString:@"Enter URl HERE"];
       NSData *myData = [NSData dataWithContentsOfURL:myUrl];
       UIImage *img = [UIImage imageWithData:myData];

       add NSURLConnection Delegate in .h File

        - (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
        [myData setLength:0];
        }

        - (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
        [myData appendData:data];
        }

        - (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        [connection release];
        }

        - (void) connectionDidFinishLoading:(NSURLConnection *)connection {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        [connection release];

        //download finished - data is available in myData.
        }
Hitesh Boricha
  • 114
  • 1
  • 11
  • I browsed and came to know that NSURLConnection will be deprecated soon.So I changed my code and used NSURLSession. In appdelegate.m file, -(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^ (UIBackgroundFetchResult))completionHandler { //my function to call server } and in viewcontroller.m file I have, [[self.session dataTaskWithURL:[NSURL URLWithString:url] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { // The control is not coming to this block.. Neither success nor failure.. } – Akshara Oct 16 '15 at 10:11
  • I am new to iOS development. and I really have no clue how this needs to be implemented. Kindly help – Akshara Oct 16 '15 at 10:12
  • Refer This tutorial http://code.tutsplus.com/tutorials/networking-with-nsurlsession-part-1--mobile-21394 – Hitesh Boricha Oct 16 '15 at 10:16
0

This is depends on OS scheduling whether it allows continue to run the services in background or kill it.

Best to use Background Fetch. Here is nice tutorial http://code.tutsplus.com/tutorials/ios-7-sdk-working-with-background-fetch--mobile-20520

Hope this solve your issue.

  • I tried the example and also tried sending a push notification. Everything worked as required. Thank you so much. But the nsurlsession is not executing. it is neither going to the success block nor the failure block. How do I download data from server? Kindly help – Akshara Oct 16 '15 at 06:45
  • NSUrlsession should work as it.. did you try with NSURLConnection? Is this service(API) working in foreground? – Waseem Ahmad Oct 16 '15 at 12:54
  • Yes... NSUrlSession and NSUrlConnection both are working in foreground. But when I tried launching the app via Background fetch... the control is neither goin to failure block nor success block. By the way I have written some log messages in the failure and success block to check if it is running in bakcground. This is sufficient to test if its working right? – Akshara Oct 18 '15 at 13:49