munna munna - 3 months ago 13
Objective-C Question

url not loading in UIWebView

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 ?

Answer

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,