UIWebView TutorialNovember 24, 2010 08:22 am Posted By: bytedissident
Adding a web page to your app is often very useful and quite easy to do. Below is a quick example.
Lets create a view based application as our starting point. Call it WebPage.
Step 1:
Create an IBOutlet for your UIWebView as we do for all UI Objects when using Interface Builder
Header file code:
@interface WebPageViewController : UIViewController {
IBOutlet UIWebView *wView;
}
@property(nonatomic,retain)IBOutlet UIWebView *wView;
@end
M (Implementation File) code:
@implementation WebPageViewController
@synthesize wView;
Step 2:
Open your XIB file in Interface Builder (if you named your project according to the project it would be WebPageViewController.xib) by double clicking on it. Drag a UIWebView from your Library onto your View.
Step 3:
Connect your UIWebView to your outlet by dragging the files owner to the UIWebView

Step 4:
Make your files owner the delegate fore your web view by ctrl-clicking on your UIWebView and dragging the line to the files owner.

Step 5:
Finally in your view controller you will need to add the code to load a web page. This is a simple process you will need to memorize. The UIWebView needs to call its loadRequest method which requires a NSURLRequest object. Here I show how you convert a URL String to a NSURLRequest Object.
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
//A URL STRING
NSString *urlAddress = @"http://bytedissident.theconspiracy5.com";
//Create a URL object FROM THAT STRING
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object CREATD FROM YOUR URL OBJECT
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[wView loadRequest:requestObj];
//scale the page to the device - This can also be done in IB if you prefer
wView.scalesPageToFit = YES;
}
Download Code
|