在iOS目标C中从前景转换为背景之后继续webservice调用

假设我在应用程序处于前台时调用web服务。 现在,如果用户发送应用程序到后台,那么我如何确保这个web服务调用在后台保持执行。

这是我在我的应用程序中使用的一段代码。

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

initiateSignInProcess有4个Web服务调用。 他们是正常的function。 我正在使用AFNetworking 。

如果有任何服务失败,我会在下面的networking代码的失败块中再次调用它:

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

现在我想知道,如果用户将应用程序发送到后台,那么代码将如何执行? 它会在bakcground调用这个函数直到成功吗?

最好使用后台进程获取。 这里是解决scheme的好教程[ http://code.tutsplus.com/tutorials/ios-7-sdk-working-with-background-fetch–mobile-20520

不可能在iOS6.x或更小,除非你的应用程序是有特定的要求,像地点,VoIP,音乐等背景运行…

但是,这是可能的iOS7,请考虑看看这个

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

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

这取决于操作系统调度是否允许继续在后台运行服务或杀死它。

最好使用后台提取。 这里是很好的教程http://code.tutsplus.com/tutorials/ios-7-sdk-working-with-background-fetch–mobile-20520

希望这能解决你的问题。