NSURLConnection Screen Lock Interrupts

I would like to know what happens behind the scenes when a user locks and unlocks the iPad screen. I have an application that downloads files using NSURLConnection, and the download fails with a SOAP error ("Could not find a server with the specified hostname"), but not when the user locks the screen, but when he unlocks it. No matter when the error appears, the download never ends. Any ideas why and what can be done about this?

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

From what I can say, when I click the Home button, I get:

 applicationWillResignActive applicationDidEnterBackground 

and after I remember the application after three minutes, I get:

 applicationWillEnterForeground 

and the download is already completed or progressing even in the background.

When I leave it in the background for longer (5 minutes), it gives an error message.

When I lock the screen, I get the same order of state of the application, but also an error message disabling download.

Thanks!

+6
source share
1 answer

I assume that your connection is disconnected because it works when the application enters the background and you did not have the correct implementations to make it work. Perhaps you should take a look at the Apple Background Execution and Multitasking documentation. It will show you how to leave the application in the background for about 10 minutes without ending it. Find the following code example and find out how it can solve your problem:

 - (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; }); } 
+6
source

All Articles