Generic-user-small Martha M. 2 posts

We are starting an iPhone programming club at our high school and I was wondering if someone could give us some coding example on how the busy indicator could be implemented in the Web Browser example.

Below taken from the prelease chapter sample on “READING DATA FROM THE NETWORK_

“could also add a “busy” indicator to indicate when the view is loading a page by providing a delegate that implements the UIWebViewDelegate protocol, which provides the callbacks webViewDidStartLoad:, webView- DidFinishLoad: and webView:didFailLoadWithError:.”

Thanks for any help you are able to give us.

Martha

 
N614592285_2406_small Chris Adamson 82 posts

Sure. As you work through the examples more, the pieces will probably become pretty self-evident to you. Just to see how much more it would take, I took a copy of the 10 minute browser and added the activity indicator. Here’s the steps to follow (pretty loose and informal, as this is a forum and not the book itself!)

  1. You’re going to be adding a UIActivityIndicator to the view, so add that to the view controller as IBOutlet UIActivityIndicatorView *activityIndicator in the .h header file.
  2. Also, while you’re here, you’ll want to indicate that the view controller can be a delegate for the web view (ie, it wants to know about the events that the web view generates), so in the angle braces where you’ve already got UITextFieldDelegate (to handle keyboard dismissal events), add a comma and then UIWebViewDelegate. So now your interface statement is @interface TenMinuteBrowserViewController : UIViewController <UITextFieldDelegate, UIWebViewDelegate>
  3. In IB, drag a UIActivityIndicator (the spinny thing) from the Library into your view. For this example, I narrowed up the URL field, moved the “Go” button left, and put the activity indicator to the right of the “Go” button. Customize the button with the inspector (I went with “white” instead of “large white”), and click the “hide when stopped” checkbox.
  4. Wire up. Connect the UIWebView’s delegate connection to File’s Owner (which, as we know, is the view controller, which we also just added the UIWebViewDelegate protocol to). Then connect File’s Owner’s “activityIndicator” outlet (which we defined in the header file in step one) to the activity indicator (the spinny thing in your view). Save your IB doc.
  5. OK, stuff is wired: the web view knows to send events to the delegate (your view controller), and your view controller knows about the activity indicator. Now you can implement the UIWebViewDelegate methods mentioned above. In my implementation, all they do is to start or stop the spinner, with the error-handler also popping up a generic error message based on the error object that gets passed to the delegate:

- (void)webViewDidStartLoad:(UIWebView *)wv {
    NSLog (@"webViewDidStartLoad");
    [activityIndicator startAnimating];
}

- (void)webViewDidFinishLoad:(UIWebView *)wv {
    NSLog (@"webViewDidFinishLoad");
    [activityIndicator stopAnimating];
}

- (void)webView:(UIWebView *)wv didFailLoadWithError:(NSError *)error {
    NSLog (@"webView:didFailLoadWithError");
    [activityIndicator stopAnimating];
    if (error != NULL) {
        UIAlertView *errorAlert = [[UIAlertView alloc]
           initWithTitle: [error localizedDescription]
           message: [error localizedFailureReason]
           delegate:nil
           cancelButtonTitle:@"OK" 
           otherButtonTitles:nil];
        [errorAlert show];
        [errorAlert release];
    }
}

And that’s it. Now when you click the “Go” button, the activity indicator appears and starts spinning. When the web page is finished loading, the spinner disappears. Your users are happy you’ve given them visual feedback as to what the app’s doing.

If you walk through this, let me know if you think I’ve missed anything…

 
Generic-user-small Martha M. 2 posts

Thanks! It worked like a charm. The students were very excited because now they can use it in the project they are working on.

 
N614592285_2406_small Chris Adamson 82 posts

Glad to hear they’re off and running!

BTW, I probably should have taken out the NSLog()s, which were just there for debugging (I often start an event-handling method with NSLog and the method name, just to make sure it’s getting called at all).

4 posts, 2 voices