Loading web page in UIWebView in Xcode 4.2 — how to connect IBOutlets properly and ensure the web page loads?

27,102

Solution 1

If you're using a storyboard, the file owner is still there but it's called View Controller. So to link the UIWebView in the storyboard to the UIWebView outlet, you hold down control, then click and drag a line from the View Controller to the Web View. This is all in the 'View Controller Scene' panel to the left of the storyboard.

Note that when you first create your project from the Single View Application template, there's no need to leave the 'Use Storyboard' checkbox ticked. You might find it easier to follow these older tutorials if you don't use a storyboard.

By the way, another important checkbox, just under 'Use Storyboard', is 'Use Automatic Reference Counting'. This is a great feature, but if you have it turned on while you're following the tutorial you've linked to, you'll need to skip the part where he releases the webView instance variable.

Solution 2

.h file

@interface webViewViewController : UIViewController <UIWebViewDelegate>

.m file

UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 640.0)];
NSURL *URL = [NSURL URLWithString:@"http://google.com"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:URL];
webView.delegate = self ;
[webView loadRequest:requestObj];
[self.view addSubview:webView];

webview is name of IBOutlet that I have created for UIWebView. To make and outlet, just Contrl + drag and drop from UIWebView to your H file between @interface and @ end.

Hope this will help you.

Share:
27,102
Crashalot
Author by

Crashalot

Hello. My friends call me SegFault. Describe myself in 10 seconds? You know those feisty, whip-smart, sometimes funny, and occasionally charming developers who dominate StackOverflow and consider Swift/Ruby/jQuery their native tongue? Yah, I buy coffee for them.

Updated on March 19, 2020

Comments

  • Crashalot
    Crashalot about 4 years

    Most tutorials on embedding a UIWebView in an iPhone app are based on older versions of Xcode. Here's an example: http://howtomakeiphoneapps.com/uiwebview-tutorial/239/

    We followed the steps in that tutorial, but the steps don't quite translate to Xcode 4.2

    There is no concept of a File Owner, for instance, but there is a "storyboard."

    Another question: how to link the UIWebView to the UIWebView IBOutlet?

    When we add the UIWebView and connect it to the ViewController, all we see is a white screen. The web page never loads.

    Could anyone share tips on loading a web page with UIWebView for Xcode 4.2?