views:

1279

answers:

3

Hi,

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!

A: 

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

Daniel
+4  A: 

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];
}
Louis Gerbarg
A: 

Thanks for the answer Louis. I have implemented the stop/startAnimating in a class called InternalBrowserView as follows:

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSLog(@"Loading Finished");
    UIApplication* app = [UIApplication sharedApplication]; 
    app.networkActivityIndicatorVisible = NO;
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
    NSLog(@"Loading Started"); 
    UIApplication* app = [UIApplication sharedApplication]; 
    app.networkActivityIndicatorVisible = YES;
}

And in my viewDidLoad method I'm doing

- (void)viewDidLoad {

    webView.delegate = self;
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.veriah.com"]]];

    [super viewDidLoad];
}

When compiling I get the following warning:

Class InternalBrowserView does not implement the 'UIWebViewDelegate' protocol

What am I missing? The application runs fine in emulator and phone.

GregoryN
Did you modify your InternalBrowserView.h header file to specify that your InternalBrowserView class implements the UIWebViewDelegate protocol?
Christopher Fairbairn
Thanks Christofer, you're correct I was missing the <UIWebViewDelegate> protocol from the interface definition in the InternalBrowserView.h file. I fixed it and now it looks like so: @interface InternalBrowserView : UIViewController <UIWebViewDelegate>
GregoryN