We start by adding a new view to the project. Drag and drop an UIWebView control on the view. As always, create an controller to handle the view, I have named the view and the view controller "WebView" and "WebViewController" respectively. We create a new object of type UIWebView and also declare an associated property. We will use this object to connect "WebView" view placed on the view. This is how the header file will look likeFileName: WebViewController.h
@interface WebViewController : UIViewController {
IBOutlet UIWebView *webView;
}
@property (nonatomic, retain) UIWebView *webView;
@end
@interface WebViewController : UIViewController {
IBOutlet UIWebView *webView;
}
@property (nonatomic, retain) UIWebView *webView;
@end
In the implementation file, do not forget to synthesize and release the object's memory. We then implement viewDidLoad method and this is how it will look like
FileName: WebViewController.m
- (void)viewDidLoad {
NSString *urlAddress = @"http://www.google.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. Method loadRequest of UIWebView is used to load the request in the UIWebView.
With some four lines of code, we can open up pages on the iPhone without using safari.
However, we do need to tell the application to load our "WebView" view when the application is finished launching.
WebViewTutorialAppDelegate.m
- (void)applicationDidFinishLaunching:(UIApplication *)application {
self.wvTutorial = [[WebViewController alloc] initWithNibName:@"WebView" bundle:[NSBundle mainBundle]];
[window addSubview:[wvTutorial view]];
// Override point for customization after app launch
[window makeKeyAndVisible];
}
Conclusion
The iPhone SDK makes it really easy to use a UIWebView object in an app. This is a good way to display your product web page in the app and not launch safari.
Happy Programming,
iPhone SDK Articles
Attachments
Suggested Readings

28 comments:
-(void)awakeFromNib {
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://i.gazzetta.it/"]]];
}
You have resolv you problem :)
Just a little question !
For example I have a tab bar controller that switches between views ! Then I put the webview on a view loaded by tha tab bar controller. How do I load the webview object then ?
Hi yodark,
You need to create a view controller for the view and load the webview in viewDidLoad method. viewWillAppear method will be called when the view gets the focus.
Select the tab item and the title in the attributes inspector should say "View Controller Identity", when you associate that view controller with the one you create (be inheriting from UIViewController) the title should say "yourname Identity"
Hope this helps.
Happy Programming
iPhone SDK Articles
Hi
This is tiwari
I have made same application as this one ,
My code do not throw any errors but when i try to run it at the time of loading a error comes
terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController loadView] loaded the "WebView" nib but no view was set.'
please resolve this
thanks and regards
sindhu tiwari
Hi, I am writing an application, where there are some topics in UItableView and once you tap a cell in table view it displays a web page in UIwebView. I could manage to display standard text on UItextView but unable to figure out how to display a web page, any help with is is highly appriciated
Where do you define wvTutorial ?
@sikosis you define the variable in WebViewTutorialAppDelegate.h file
Happy Programming,
iPhone SDK Articles
I am trying to do what yodark was talking about, but am having difficulty in doing so. when i run it, it shows the app like you have created but not inside the tab bar view i have linked it to. any suggestions?
@katana So does the webview show up as its own view? Try selecting a tab bar item, drag and drop a view object and drag and drop a UIWebView on top of it. Now the webview should show up on the view which is placed on the selected tab bar item.
Hope it helps. Let me know how it works out.
Happy Programming,
iPhone SDK Articles
Thanks for the help. the problem was that in my projects delegate i had coded the tabbarcontroller to be a subview of the window before the webview, when i swapped these two around, it worked fine. Thanks, I'd just like to say your tutorials are great!
@katana Glad it all worked out for you and that you enjoyed the articles. You can follow me on twitter here http://twitter.com/iphonearticles to get updates on the site.
Happy Programming,
iPhone SDK Articles
Can someone give an example of how to declare the wvTutorial variable. I can't figure this out for the life of me. Sorry, I'm a beginner.
@Chad In WebViewTutorialAppDelegate.h file declare a variable like this inside the braces
WebViewController *wvTutorial;
Outside the braces create a property like this
@property (nonatomic, retain) WebViewController *wvTutorial;
You also need to add the forward class declaration like this
@class WebViewTutorialViewController, WebViewController;
You can download the source code for this tutorial here
http://sites.google.com/site/iphonesdktutorials/sourcecode/UIWebViewTutorialPart1.zip
Sorry you had some trouble with this, let me know if I can help more.
Happy Programming,
iPhone SDK Articles
I have a UIWebView as part of a navigation controller. Sometimes when the user navigates back (popping the view), I get an error message if the page hadn't finished loading yet:
/SourceCache/WebCore/WebCore-351.9.42/wak/WKWindow.c:250 WKWindowIsSuspendedWindow: NULL window.
Is there something special to do to shut it down cleanly?
How does one place a UIWebView as a subview of a UIView or into a table of a UITableView?
For instance say I wanted to place a WebView inside the infoView in your Navigation Controller + UIToolbar tutorial.
@Bryan McWhirt You can just drag and drop the UIWebView object from the Interface Builder Library on to the UIView.
Please let me know if I can be of more help.
Happy Programming,
iPhone SDK Artiles
@Wendell Hicken
I haven't experienced that issue before. Is it possible for you to send me the source code so I can take a look at it?
Happy Programming,
iPhone SDK Articles
I tracked this down myself. Adding the following code seems to keep it happy:
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
webView.delegate = nil;
[webView stopLoading];
}
Thanks for the tutorial. It really helped. Since EDGE is so slow, how could I make a progress bar set to the loading status of the webView? I know how to make a progress bar, but I can't figure out how to set it to the progress of the webView. Any ideas?
Code:
//Create an otherwise-empty alert sheet
alert = [[UIAlertSheet alloc]
initWithTitle:@"Loading Site"
buttons:NULL
defaultButtonIndex:0
delegate:self
context:self];
[alert setBodyText:@"Please wait...\n\n\n\n"];
// Create the progress bar and add it to the alert
progbar = [[UIProgressBar alloc] initWithFrame:
CGRectMake(50.0f, 70.0f, 220.0f, 90.0f)];
[alert addSubview:progbar];
[progbar setStyle: STYLE];
[alert presentSheetInView:imgView];
Now how would I set the progress bar to the webView?
Thanks for the great tutorial, but how would I create a progress bar showing the progress of the webView?
How do I make the change the webview size so that the iphone status bar does not cut off the top 20 pixels of the webpages? I tried changing the status bar from 'none' to 'gray' for the View, but does not make a difference...
Tiwari, in initWithNibName, check your string to make sure you don't have something like "*** View". Try joining them into one word. For some reason, you have to be careful about what strings you put in the initWithNibName method.
I just noticed that when something inside the webview is clicked, nothing happens. How would you allow clicks to take you to another site from this webview?
@Staff A UIWebView is meant to load web pages and the user will navigate to another page using the hyperlinks in the page.
Hope this helps.
Happy Programming,
iPhone SDK Articles
Hi,
was wondering if someone could help me with this.
When I try running this, I get this error -
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[WeatherAppAppDelegate 0x5234e0 setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key viewController.'
Thanks,
Ketan
An advance pardon on my behalf, I'm really new to this. This might be an easy question, but I build an in-house web app. It loads the page, but none of my JavaScript or the server login page work or pop up. Am I doing something wrong? Breaking any obvious rules? *Probably.
Thanks for the tutorial. I'll try this out and see if it works. Could be useful for my app.
@ Anonymous
Open Interface builder and grab UIWebView and scale from the top down to 460 tall.
Worked for me.
Post a Comment