#import "ViewController.h" @interface ViewController ()<UIWebViewDelegate> @property (weak, nonatomic) IBOutlet UIWebView *webView; @property (weak, nonatomic) IBOutlet UIBarButtonItem *goBack; @property (weak, nonatomic) IBOutlet UIBarButtonItem *goForward; @end @implementation ViewController #pragma mark ---------------------- #pragma mark Life Cycle - (void)viewDidLoad { [super viewDidLoad]; NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; //加载网页 [self.webView loadRequest:request]; //设置代理 self.webView.delegate = self; } #pragma mark ---------------------- #pragma mark Events - (IBAction)goBackBtnClick:(id)sender { [self.webView goBack]; } - (IBAction)goForwardBtnClick:(id)sender { [self.webView goForward]; } - (IBAction)reloadBtnClick:(id)sender { [self.webView reload]; } #pragma mark ---------------------- #pragma mark UIWebViewDelegate //即将加载某个请求的时候调用 -(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { NSLog(@"%@",request.URL.absoluteString); //简单的请求拦截处理 NSString *strM = request.URL.absoluteString; if ([strM containsString:@"360"]) { return NO; } return YES; } //1.开始加载网页的时候调用 -(void)webViewDidStartLoad:(UIWebView *)webView { NSLog(@"webViewDidStartLoad"); } //2.加载完成的时候调用 -(void)webViewDidFinishLoad:(UIWebView *)webView { NSLog(@"webViewDidFinishLoad"); self.goBack.enabled = self.webView.canGoBack; self.goForward.enabled = self.webView.canGoForward; } //3.加载失败的时候调用 -(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { NSLog(@"didFailLoadWithError"); } @end
#####8 WebView的基本使用
```objc
1 概念性知识
01 webView是有缺点的,会导致内存泄露,而且这个问题是它系统本身的问题。
02 手机上面的safai其实就是用webView来实现的
03 现在的开发并不完全是原生的开发,而更加倾向于原生+Html5的方式
04 webView是OC代码和html代码之间进行交互的桥梁
2 代码相关
/*A*网页操控相关方法**/
[self.webView goBack]; 回退
[self.webView goForward]; 前进
[self.webView reload]; 刷新
//设置是否能够前进和回退
self.goBackBtn.enabled = webView.canGoBack;
self.fowardBtn.enabled = webView.canGoForward;
/*B*常用的属性设置**/
self.webView.scalesPageToFit = YES; 设置网页自动适应
self.webView.dataDetectorTypes = UIDataDetectorTypeAll; 设置检测网页中的格式类型,all表示检测所有类型包括超链接、电话号码、地址等。
self.webView.scrollView.contentInset = UIEdgeInsetsMake(50, 0, 0, 0);
/*C*相关代理方法**/
//每当将加载请求的时候调用该方法,返回YES 表示加载该请求,返回NO 表示不加载该请求
//可以在该方法中拦截请求
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
return ![request.URL.absoluteString containsString:@"dushu"];
}
//开始加载网页,不仅监听我们指定的请求,还会监听内部发送的请求
-(void)webViewDidStartLoad:(UIWebView *)webView
//网页加载完毕之后会调用该方法
-(void)webViewDidFinishLoad:(UIWebView *)webView
//网页加载失败调用该方法
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
/*D*其它知识点-加载本地资源**/
NSURL *url = [[NSBundle mainBundle] URLForResource:@"text.html" withExtension:nil];
[self.webView loadRequest:[NSURLRequest requestWithURL:url]];
```
#####9 HTML
```objc
1.Html决定网页的内容,css决定网页的样式,js决定网页的事件
2.html学习网站:http://www.w3school.com.cn