锁屏中断NSURLConnection

当用户locking和解锁iPad屏幕时,我想知道幕后发生了什么。 我有一个应用程序,使用NSURLConnection下载文件和下载失败与SOAP错误(“具有指定主机名的服务器无法find”),但不是当用户locking屏幕,但当它解锁它。 不pipepopup错误的时候,下载都不会结束。 任何想法为什么和可以做些什么呢?

NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:300]; NSURLConnection* conn = [[NSURLConnection alloc] initWithRequest: request delegate: self]; 

从我可以告诉的是,当我点击主页button,我得到:

 applicationWillResignActive applicationDidEnterBackground 

在三分钟后我回想起应用程序后,我得到:

 applicationWillEnterForeground 

并且下载或者已经完成或者甚至在后台进行。

当我在后台更长的时间(5分钟),它超时出错。

当我locking屏幕时,我得到相同的应用程序状态的顺序,但也是关于下载断开的错误消息。

谢谢!

我的猜测是你的连接正在断开连接,因为它在应用程序进入后台时正在运行,而你没有正确的实现来保持它的运行。 也许你应该看看苹果的后台执行和多任务处理文档。 它会告诉你如何让你的应用程序在后台运行10分钟而不被终止。 查找以下示例代码,并了解它是如何解决您的问题的:

 - (void)applicationDidEnterBackground:(UIApplication *)application { bgTask = [application beginBackgroundTaskWithExpirationHandler:^{ // Clean up any unfinished task business by marking where you. // stopped or ending the task outright. [application endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }]; // Start the long-running task and return immediately. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // Do the work associated with the task, preferably in chunks. [application endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }); }