Reputation: 3315
I'm using objective-c & the iphone 3.0 sdk
I want to retrieve the contents of a URL (web page or pdf) and display it within a UIWebView
it seems that the thing to do is something like this:
NSURLRequest *requestObject = [NSURLRequest requestWithURL:myURL];
[myUIWebView loadRequest:requestObject];
this works quite well, except for 1 thing.
there are cases when the request takes a while (let's say 5-10 seconds). I have a UIActivityIndicatorView that I'd like to "start" and "stop" at the beginning and end of the load request.
HOWEVER, if I do this
[busySignal startAnimating];
NSURLRequest *requestObject = [NSURLRequest requestWithURL:myURL];
[myUIWebView loadRequest:requestObject];
[busySignal stopAnimating];
it doesn't work because the loadRequest call is done asynchronously and so my UIActivityIndicatorView stops almost immediately
Is there a way to start/stop my "busySignal" at the beginning and end of a URL request?
As an alternative, I think this can be done via NSURLConnection which gives me the callback function "connectionDidFinishLoading:"
but I can't seem to figure out how to go from NSURLConnection to displaying the web-page.
So, I guess that's 2 questions.
1. Can you start/stop a UIActivityIndicatorView at the beginning/end of a call to loadRequest: ? 2. How do you use NSURLConnection to retrieve the contents of a URL and display the information on a UIWebView?
Thanks!
Upvotes: 1
Views: 4788
Reputation: 43452
You really don't want to do the load synchronously right there, if you do your UI will jam for multiple seconds while you load. If your main run loop stops for too long (slow internet connection) the system will assume you have crashed and killed your app.
You want to use the UIWebViewDelegate methods in order to be informed when it is doing something. For instance:
myUIWebView.delegate = self;
NSURLRequest *requestObject = [NSURLRequest requestWithURL:myURL];
[myUIWebView loadRequest:requestObject];
and then in you implement the following delegate methods:
//Called whenever the view starts loading something
- (void)webViewDidStartLoad:(UIWebView *)webView {
[busySignal startAnimating];
}
//Called whenever the view finished loading something
- (void)webViewDidFinishLoad:(UIWebView *)webView_ {
[busySignal stopAnimating];
}
Upvotes: 5
Reputation: 22395
Look at the UIWebViewDelegate protocol should have what u need, it has requestdidstart loading and such methods that are there for exacty what u want to do
Upvotes: 0