How to use UIWebView in iPhone?

This post deals with using UIWebView in iPhone applications. You can use webview to load web contents in your application.

1) Lets say we have a view controller class “webViewController”. Open webViewController.h file and add the following lines of code.

#import <UIKit/UIKit.h>
@interface webViewController : UIViewController {
	IBOutlet UIWebView *WView;
}

@end

2) Now come to webViewController .m file and add the following lines of code in “viewDidLoad” method.

NSURL *URL = [NSURL URLWithString:@"http://www.google.com"];

	NSURLRequest *requestObj = [NSURLRequest requestWithURL:URL];

	[WView loadRequest:requestObj];

3) As we will be making this class as the delegate for UIWebView so add the delegate method, as shown below, which is called when UIWebView finishes loading of content.

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
	NSLog(@"finished download");
}

4) Now open webViewController.xib for adding objects to view and making connections as shown in figure below.

In the above xib we have added

a) A UIWebView which will show the web content.

b) 4 Buttons

(i) Back- for moving back to previous page in back-forward list. Connected to action “goBack” of UIWebView as shown.

(ii) Forward- for moving to next page in back-forward list. Connected to action “goForward” of UIWebView as shown.

(iii) Stop- to stop loading of request. Connected to action “stopLoading” of UIWebView as shown.

(iv) Reload- to reload the request. Connected to action “reload” of UIWebView as shown.

c) A Label saying “How to use UIWebView” which you may or may not add.

5) Connect the outlet WView to UIWebView. Also make the class as a delegate for UIWebView by making connection between delegate and File’s Owner.

6) Save, Build and Run the Project.

7) Output is as shown below.