1

I have numerous UIWebViews in my iOS (versions 5-6.2) application. When the app enters the background, everything runs smoothly. However, when it enters the foreground after ~10 minutes, I get an error message either saying something like "a valid hostname cannot be found" or "connection timeout."

I am assuming this has to do with my lack of action towards these UIWebViews when applicationDidEnterBackground: gets called. How would I be able to kill these connections? I understand I need to use notification center, but unlike previous questions like this, I am using ARC so there's no dealloc method in which I can remove observers.

EDIT

Here is some of my web view code: WebViewController.m

NSURLRequest *request = [NSURLRequest requestWithURL:urlToLoad  cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0f];
// load the request to the UIWebView _webView
[_webView loadRequest:request];

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:TRUE];
if (connection) {
    receivedData = [NSMutableData data];
}

Any help is greatly appreciated. Thank you.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
eulr
  • 171
  • 2
  • 19
  • Do the pages that you load request something periodically? If you actually see the errors as message boxes, they are likely coming from javascript on the page. – SVD Apr 11 '13 at 04:06
  • No, they do not request something periodically. The message boxes that appear are simply UIAlertViews that I have set up when the connection fails. But rather than populating that message with something I write, I just use the localizedDescription of NSError. – eulr Apr 11 '13 at 04:08
  • Do they finish loading before you move the app into background? If not, it maybe worth it to request some background time from OS, as the other answer suggests. If you just want to stop loading, does [webview stopLoading] work? – SVD Apr 11 '13 at 04:15
  • Perhaps paired with [webView reload] when back in foreground, if you had to stop loading when going into background – SVD Apr 11 '13 at 04:16

1 Answers1

0

if you are performing some Important or Precious Downloading or Uploading operation and your app enters background, in that case you can request for Additional time from 'IOS' to finish your work, it grants additional 10 minutes to finish that while your app is in background mode.

But Keep in Mind that, your Operation must be important and Acceptable, otherwise your app may get Rejected from Apple Review Process.

For More Details Please Refer Apple Document for Background Execution and Multitasking

Now, Concluding my Point and time for some Action, to Continue your Task in Background you can perform with following method, no need to manage Application Delegate method. just use following snippet and dont use delegate in that for downloading or uploading.

if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)]) { //Check if our iOS version supports multitasking I.E iOS 4
    if ([[UIDevice currentDevice] isMultitaskingSupported]) { //Check if device supports mulitasking
        UIApplication *application = [UIApplication sharedApplication]; //Get the shared application instance
        __block UIBackgroundTaskIdentifier background_task; //Create a task object
        background_task = [application beginBackgroundTaskWithExpirationHandler: ^ {
            [application endBackgroundTask: background_task]; //Tell the system that we are done with the tasks
            background_task = UIBackgroundTaskInvalid; //Set the task to be invalid
            //System will be shutting down the app at any point in time now
        }];
        //Background tasks require you to use asyncrous tasks
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            //Perform your tasks that your application requires
            NSLog(@"\n\nRunning in the background!\n\n");

            NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"YOUR HOST URL"]];
            NSURLResponse *response = nil;
            NSError *requestError = nil;
            NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError];

            NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
            NSLog(@"ResponseString:%@",responseString);

            [application endBackgroundTask: background_task]; //End the task so the system knows that you are done with what you need to perform
            background_task = UIBackgroundTaskInvalid; //Invalidate the background_task
        });
    }
}
Dipen Panchasara
  • 13,480
  • 5
  • 47
  • 57
  • Hmmm. This seems quite hefty and tailored to for apps that need to do something after they enter the background. I don't want to do something, I just want to stop loading the various web views or all connections in general. – eulr Apr 11 '13 at 04:12
  • in webview there is no such mechanism available, as far my knowledge concern, but your can stop NSURLConnection with NSURLConnection *cn = [NSURLConnection connectionWithRequest:request delegate:nil]; [cn cancel]; – Dipen Panchasara Apr 11 '13 at 04:16
  • how can I call a global instance variable from one View Controller in the AppDelegate? Would it be efficient to import all of those headers? – eulr Apr 11 '13 at 04:30
  • yes you can or you can make Singleton to manage Globle Variables. – Dipen Panchasara Apr 11 '13 at 04:41