Continue webservice call after transition from foreground to background in iOS objective C

我与影子孤独终老i 提交于 2019-12-13 05:20:15

问题


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?


回答1:


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




回答2:


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/




回答3:


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



回答4:


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.



来源:https://stackoverflow.com/questions/33140427/continue-webservice-call-after-transition-from-foreground-to-background-in-ios-o

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!