Apple iPhone Web Kit with Activity Indicator


Welcome to the club of searching for an overly simple UIWebView a.k.a. WebKit example! In this example, I’ll show you simply how to hand code a quick UIWebView into your program as well as to add a UIActivityIndicatorView a.k.a. an activity indicator. Without jabbing Apple too hard here, the documentation is pretty bad and that is why it’s nice to have an example just shown to you as-is. I hope this example helps shine a light on the situation for anyone wanting to implement a nice and quick Apple iPhone WebKit solution.

 
#import <UIKit/UIKit.h>
 
@interface FirstViewController : UIViewController <UIWebViewDelegate>
{
	UIWebView *myWebView;
	UIActivityIndicatorView *activityIndicator;	
}
/*
Inside the @implementation FirstViewController ... 
*/
- (void)viewDidLoad { //We have a NIB file in play here, so I dropped the loadView here.  Just make sure that your loadView is not getting called twice!
    [super viewDidLoad];
    [self loadView];
}
 
- (void)loadView {
	UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
	self.view = contentView;	
 
	CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
	webFrame.origin.y = 0.0f;
	myWebView = [[UIWebView alloc] initWithFrame:webFrame];
	myWebView.backgroundColor = [UIColor blueColor];
	myWebView.scalesPageToFit = YES;
	myWebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
	myWebView.delegate = self;
	[self.view addSubview: myWebView];
	[myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.maxpowersoft.com/"]]];
 
	activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
	activityIndicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
	activityIndicator.center = self.view.center;
	[self.view addSubview: activityIndicator];
}
 
- (void)dealloc {
	[activityIndicator release];
	[myWebView release];
        [super dealloc];
}
 
#pragma mark WEBVIEW Methods
 
- (void)webViewDidStartLoad:(UIWebView *)webView
{
	// starting the load, show the activity indicator in the status bar
	[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
	[activityIndicator startAnimating];
}
 
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
	// finished loading, hide the activity indicator in the status bar
	[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
	[activityIndicator stopAnimating];
}
 
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
	// load error, hide the activity indicator in the status bar
	[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
 
	// report the error inside the webview
	NSString* errorString = [NSString stringWithFormat:
							 @"<html><center><br /><br /><font size=+5 color='red'>Error<br /><br />Your request %@</font></center></html>",
							 error.localizedDescription];
	[myWebView loadHTMLString:errorString baseURL:nil];
}

That is all there is to it. It’s really simple as you can see. Feel free to copy and paste accordingly.

, , , , ,

  1. #1 by chris on December 13th, 2009

    Hi Chris,
    sounds cool, also I still have problems to get it run.
    can you post a demo to install. would be great.

    I already have a working webview, but it makes in some cases problems. Does yours easy can autorotate for example? .. a demo to download.. really.. please :)

    greets
    Chris

  2. #2 by chris on December 13th, 2009

    Hi Chris,
    would be really a help to come in contact.
    Finaly I got your source work. Its kind similar with what i had already just done with the Interface Builder.. and I am happy now I have it just by Code.

    But also I have the same Problem :)
    For example when I activate
    shouldAutorotateToInterfaceOrientation
    return TRUE,

    so it allows just all directions, it works cool.. till i come to a form field where I enter anything… when I am than in landscape Mode and scroll.. the Scrollbar appears in the middle of the screen… just like the view itself is not landscape, instead still portrait.. but the webpage itself is shown correct in landscape…

    I would even provide you with my source.. but anyhelp i appreciate.. i am nearly done with a project for a client.. and now that makes me so much headache. I would for sure sponsor you a beer through paypal :)

    Thx
    Chris

    When I activate shou

  3. #3 by forex robot on December 17th, 2009

    Keep posting stuff like this i really like it.

(will not be published)

  1. No trackbacks yet.