在iOS9.0之后需要在ifo.plist文件中添加一个配置文件,不然只能识别https开头的网址,http开头的不识别
主要有三个步骤
// 1.获取URL
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"]; // 2.发送请求
NSURLRequest *request = [NSURLRequest requestWithURL:url]; // 3.加载内容
[self.webView loadRequest:request];
还有一些代理方法:
#import "RootViewController.h"
#import "RootView.h" @interface RootViewController () <UIWebViewDelegate>
@property (nonatomic, strong) RootView *rootView; @end @implementation RootViewController - (void)loadView {
self.rootView = [[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.view = self.rootView;
} - (void)viewDidLoad {
[super viewDidLoad]; // 设置代理
self.rootView.webView.delegate = self; // 添加按钮事件
[self.rootView.button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside]; [self.rootView.button1 addTarget:self action:@selector(click1:) forControlEvents:UIControlEventTouchUpInside];
} #pragma mark - 代理方法
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { NSLog(@"加载失败");
} - (void)webViewDidFinishLoad:(UIWebView *)webView { NSLog(@"加载完成"); // 获取输入的内容
NSString *str = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
NSLog(@"title = %@", str);
} - (void)webViewDidStartLoad:(UIWebView *)webView { NSLog(@"开始加载");
} #pragma mark - 实现按钮点击事件(返回上一页,前进到下一页)
- (void)click:(UIButton *)sender { // 返回上一页
[self.rootView.webView goBack];
} - (void)click1:(UIButton *)sender { // 返回上一页
[self.rootView.webView goForward];
}
@end