20

i have a tab bar control. 1st tab contains a navigation control. On the 2nd tab i want to load a web page (say google.com). I have written the code as

NPIViewController.h

@interface NPIViewController : UIViewController {
   IBOutlet UIWebView *webView;
}
@property (nonatomic,retain) IBOutlet UIWebView *webView;
@end

NPIViewController.m

- (void)viewDidLoad {
  [super viewDidLoad];
  NSURL *url = [NSURL URLWithString:@"http://www.google.com"]; 
  NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
  [webView setScalesPageToFit:YES];         
  [self.webView loadRequest:request];               
}

The page just does not load. No compilation or runtime errors. What is wrong in this ?

Michaël
  • 6,676
  • 3
  • 36
  • 55
Ashish K Agarwal
  • 1,172
  • 3
  • 17
  • 33

4 Answers4

36

to know whats wrong you can do this.

- (void)viewDidLoad {
  [super viewDidLoad];
  webView.delegate = self;
  NSURL *url = [NSURL URLWithString:@"http://www.google.com"]; 
  NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
  [webView setScalesPageToFit:YES];         
  [self.webView loadRequest:request];               
}

add this new method in your class

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
   NSLog(@"Error : %@",error);
}

I hope you have connected webView object with its outlet in Interface builder?

Thanks,

pkamb
  • 33,281
  • 23
  • 160
  • 191
Ravin
  • 8,544
  • 3
  • 20
  • 19
  • Ravin, i added this method. but there is nothing on the console. No page load. I have also made a connection to webView from the IB. – Ashish K Agarwal Apr 13 '11 at 06:44
  • is webView is declared as property? if not then instead of using [self.webView loadRequest:request]; use [webView loadRequest:request]; this.(remove self. from self.webView) thanks – Ravin Apr 13 '11 at 06:47
  • In the .h file i have made the webView as a property. Can you verify the code i have mentioned? Is it correct? I just took it from example codes on net. – Ashish K Agarwal Apr 13 '11 at 06:50
  • have you set delegate as I have written in above code webView.delegate = self? try without self.webView, use webView. – Ravin Apr 13 '11 at 06:53
  • what about delegate setting? NSLog(@"URL : %@",url); – Ravin Apr 13 '11 at 07:03
  • I got this working. Actually the mistake was on my part. In the IB, i had wrongly connected the webView with the UIView object. The correct thing is to connect the UIview with the view & the webView with the UIWebView object. Thanks @Ravin for pointing out. – Ashish K Agarwal Apr 13 '11 at 07:06
  • 2
    FOR FUTURE GOOGLERS: To remove compiler warning, you may want to implement in your AppDelegate interface. – EwyynTomato Mar 19 '12 at 09:17
3

You have to use https instead of http. If you use http you will get this error message (use Ravin's code to see the error):

"The resource could not be loaded because the App Transport Security policy requires the use of a secure connection."

user1139733
  • 986
  • 9
  • 14
2

Create you webView

IBOutlet UIWebView *webView;

Try this code

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];
raed
  • 4,887
  • 4
  • 30
  • 49
Gypsa
  • 11,230
  • 6
  • 44
  • 82
0

Restart your IOS simulator.
It is really not evident, but at first check some site in Safari at IOS simulator.
After restart of IOS simulator, my webView opened successfully both at simulator and device.

See this famous link.

Community
  • 1
  • 1
rock_walker
  • 453
  • 5
  • 14