Sunday, May 1, 2011

UIWebView Tutorial

Hello Friends,

Now we will use excellent feature of iOS SDK. In iOS apple has provided us ready made library for the web interaction. It is so finely designed that all the desired features are included in library. We can easily make browser application with this feature.

Now we will write a code to open www.apple.com website in application. We will start this work by creating View Based Application.

  • Create View Based Application and Give a name to it like WebView.
  • Open WebViewViewController.xib file in interface builder.
  • Drag a Web View from library to main View.
  • Now open WebViewController.h file and write a following code. 
@interface WebViewController : UIViewController {
IBOutlet UIWebView *webView;
}

@property (nonatomic, retain) UIWebView *webView;

@end
  • Now Save the header file and open interface builder again for connecting outlet again to the UIWebView which we have added just before to the main view.
  • After Connecting outlet to the UIWebView Controller, We can use that WebView in implementation file.
  • Do not forget to synthesize webView object in Implementation file. @synthesize method set the setter method for the webView object.
  • Now open WebViewController.m file and add the following code. 
- (void)viewDidLoad {
NSString *urlAddress = @”http://www.apple.com”;

//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];

//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

//Load the request in the UIWebView.
[webView loadRequest:requestObj];
}

  •  NSURL class is used to create an object which will hold the URL information.
  •  NSURLRequest is used to create a request to the URL.
  •  LoadRequest  method of UIWebView is used to load the request in the UIWebView.
Now Build and Run Program you should be able to see www.apple.com page. :-)

    No comments:

    Post a Comment