UIWebView
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]];
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self test1];
}
-(void)test1
{
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
//加载网页
[self.webView loadRequest:[NSURLRequest requestWithURL:url]];
}
-(void)test2
{
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
//加载网页
[self.webView loadRequest:[NSURLRequest requestWithURL:url]];
self.webView.scrollView.contentInset = UIEdgeInsetsMake(40, 0, 0, 0);
}
//加载本地的文件
-(void)test3
{
NSURL *url = [NSURL fileURLWithPath:@"/Users/xiaomage/Desktop/07-NSURLSession.pptx"];
//加载网页
[self.webView loadRequest:[NSURLRequest requestWithURL:url]];
}
-(void)test4
{
NSURL *url = [NSURL URLWithString:@"http://www.520it.com/"];
//加载网页
[self.webView loadRequest:[NSURLRequest requestWithURL:url]];
//设置时候自适应
self.webView.scalesPageToFit = YES;
}
-(void)test5
{
NSURL *url = [[NSBundle mainBundle] URLForResource:@"text.html" withExtension:nil];
//加载网页
[self.webView loadRequest:[NSURLRequest requestWithURL:url]];
//设置时候自适应
self.webView.dataDetectorTypes = UIDataDetectorTypeAll;
}
@end
#import "ViewController.h"@interface ViewController ()@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