开发者

url not loading in UIWebView

开发者 https://www.devze.com 2023-02-25 00:24 出处:网络
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

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 ?


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,


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."


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];


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消