我有一个选项卡栏控件。第一个选项卡包含一个导航控件。在第二个选项卡上,我想加载一个网页(比如google.com)。我已经将代码写成
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];
}
页面就是不能加载。没有编译或运行时错误。这里面有什么问题呢?
发布于 2011-04-13 14:35:58
要知道哪里出了问题,你可以这样做。
- (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];
}
在您的类中添加此新方法
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSLog(@"Error : %@",error);
}
我希望你已经在接口构建器中连接了webView对象和它的插座?
谢谢,
发布于 2016-04-15 22:07:09
您必须使用https而不是http。如果你使用http,你会得到这个错误消息(使用Ravin的代码来查看错误):
“无法加载资源,因为应用程序传输安全策略要求使用安全连接。”
发布于 2011-04-13 14:59:33
创建你的webView
IBOutlet UIWebView *webView;
试试这段代码
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];
https://stackoverflow.com/questions/5645275
复制相似问题