tags:

views:

772

answers:

4

I have an UITabBar based iPhone app with 4 different UIWebViews under every tab. Right now all the UIWebViews load on startup and it takes a little bit too long.

I would like to load the default tab's UIWebView first, then load the others in the background. What is the best way to do this?

I have seperate ViewControllers set up for each tab and have this in every .m file:

- (void)viewDidLoad {

NSString *urlAddress2 = @"http://google.com ";

//Create a URL object.
NSURL *url2 = [NSURL URLWithString:urlAddress2];

//URL Requst Object
NSURLRequest *requestObj2 = [NSURLRequest requestWithURL:url2];

//Load the request in the UIWebView.
[webView2 loadRequest:requestObj2];

}

Is there a simple way to tell the other 3 tabs to start loading a few seconds after launch instead of at launch? Would that be a good idea?

Thanks a lot!

A: 

You can use NSTimer, or do the loading in viewDidAppear or similar.

Can Berk Güder
+1  A: 

If you use ASIHTTPRequest instead of NSURLRequest, you can fire a synchronous request for the first URL. Once that request is complete, you can then fire off the other three URL requests asynchronously (i.e., in the background).

Alex Reynolds
A: 

Use viewDidAppear. This will be sent to the controller after the view fully appears and animations end.

Benjamin Ortuzar
A: 

I've gotten around this issue by implementing a model layer through which all requests pass. Requests are queued and serviced in priority order, generally one at a time. I've added specific methods to allow a controller to escalate the priority of requests so that, if necessary, two or more requests will be active at once. When a request finishes, it alerts it delegate (the WebView's controller) that data is ready to be loaded.

Depending on how you want to set things up, you can put a callback in "webViewDidFinishLoad" (or, perhaps, shouldStartLoadWithRequest or webViewDidStartLoad) that triggers the model layer to dequeue and service the next request. For safety, you'll also want a timeout in the model layer.

Note: you'll also need to add some custom code into shouldStartLoadWithRequest to differentiate between clicks and the model layer pushing data in. I.e. you'll want to return NO or YES depending on the navigationType.

D Carney