Search code examples
iphoneiosuiweb

How can I add navigation to embedded UIWebView?


Im using the UIWebView to access a mobile website from within my iPhone application. I need help adding the forward and back navigation buttons. Im really new to this so if someone could provide some good details I would really appreciate your help.

Here is what I have:

UIWebView *webView1 = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 480, 251)];
[contentView addSubview:webView1];
webView1.alpha = 1.0;
webView1.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[webView1 loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.logsitall.com/m"]]];
webView1.scalesPageToFit = YES;
[webView1 release];


contentView.frame = self.view.bounds;
[self.view addSubview:contentView];
[contentView release];
}

- (void)viewDidUnload {
    [self setWebView:nil];
    [super viewDidUnload];
}



- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:animated];
}


@end

Final code:

//header
@interface MywebsiteViewController : UIViewController <UIWebViewDelegate>
{
    IBOutlet UIWebView *aWebView;
}

@property (nonatomic, retain) UIWebView *aWebView;


@end

//implementation

- (void)viewWillAppear:(BOOL)animated {
    self.aWebView.delegate = self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURL *myURL = [NSURL URLWithString:@"http://www.WebSiteToLoad.com"];
    NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];

    [aWebView loadRequest:myRequest];

}


#pragma mark UIWebViewDelegate

- (void)webViewDidStartLoad:(UIWebView *)webView{
    //show indicator while loading website
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    //turn off indicator and display the name of the website in the navBar
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    self.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];

}

Solution

  • There are a lot of UIWebView tutorials on.....wait for it....The Web! That said, as I recall, it's as simple as calling goForward or goBack on your WebView:

    [[self mainWebView] goForward];