UIWebView 是苹果提供的用来展示网页的UI控件,它也是最占内存的控件。
iOS8.0之后出现了webkit框架,WKWebView相比UIWebView节省了1/4~1/3的内存,速度快,但是没缓存功能。
对于一些购物类app网页的展示是必不可免的,因此UIWebView对于我们来说也是必须精通的控件。
下面给大家先简单展示一下UIWebView。
1 // 2 // ViewController.m 3 // UIWebView 4 // 5 6 7 #import "ViewController.h" 8 9 @interface ViewController ()<UIWebViewDelegate> 10 11 @property (nonatomic, strong) UIWebView * webView; 12 13 @end 14 15 @implementation ViewController 16 17 - (void)viewDidLoad { 18 [super viewDidLoad]; 19 //初始化webView 20 _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, CGRectGetHeight(self.view.frame) - 60)]; 21 22 NSURL * url = [NSURL URLWithString:@"http://www.baidu.com"]; 23 24 [_webView loadRequest:[NSURLRequest requestWithURL:url]]; 25 //把webView添加到View上 26 [self.view addSubview:_webView]; 27 28 } 29 /* 30 - (void)reload; 刷新 31 - (void)stopLoading; 停止load 32 33 - (void)goBack; 返回 34 - (void)goForward; 前进 35 */ 36 @end
运行结果
接下来我们来一个Webkit的demo
1 // 2 // ViewController.m 3 // 句子迷 4 // 5 // Created by ma c on 16/4/7. 6 // Copyright © 2016年 晓宁. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 #import <WebKit/WebKit.h> 11 @interface ViewController () 12 @property(nonatomic,strong)WKWebView*webView; 13 @end 14 15 @implementation ViewController 16 17 - (void)viewDidLoad { 18 [super viewDidLoad]; 19 self.webView=[[WKWebView alloc]initWithFrame:self.view.bounds]; 20 [self.view addSubview:self.webView]; 21 [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.juzimi.com"]]]; 22 // Do any additional setup after loading the view, typically from a nib. 23 } 24 25 - (void)didReceiveMemoryWarning { 26 [super didReceiveMemoryWarning]; 27 // Dispose of any resources that can be recreated. 28 } 29 30 @end
展示结果如下