注意點(diǎn): WKWebView只支持iOS8以上且不支持xib,切勿使用xib進(jìn)行拖拽
WKWebview 優(yōu)勢:
1.更多的支持HTML5的特性
2.官方宣稱的高達(dá)60fps的滾動(dòng)刷新率以及內(nèi)置手勢
3.將UIWebViewDelegate與UIWebView拆分成了14類與3個(gè)協(xié)議,以前很多不方便實(shí)現(xiàn)的功能得以實(shí)現(xiàn)。文檔
4.Safari相同的JavaScript引擎
5.占用更少的內(nèi)存
基本使用方法:
WKWebView有兩個(gè)delegate,WKUIDelegate和WKNavigationDelegate。WKNavigationDelegate主要處理一些跳轉(zhuǎn)、加載處理操作,WKUIDelegate主要處理JS腳本,確認(rèn)框,警告框等。
#import <WebKit/WebKit.h>
@interface ZebWebViewController()<WKNavigationDelegate,WKUIDelegate,WKScriptMessageHandler>
@property (nonatomic, strong) UIProgressView *progressView;
@property (nonatomic,strong) WKWebView *wkwebView;
@end
@implementation ZebWebViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self SetUI];
}
-(void)SetUI{
[self setWKWebView];
[self setUIProgressView];
}
#pragma mark webview初始化
-(void)setWKWebView{
WKWebViewConfiguration *config=[[WKWebViewConfiguration alloc]init];
// 不開有視頻就不能播放
config.allowsInlineMediaPlayback=YES;
config.preferences=[[WKPreferences alloc]init];
// 最小字體為10號
config.preferences.minimumFontSize = 10;
config.preferences.javaScriptEnabled = YES;
// 默認(rèn)是不能通過JS自動(dòng)打開窗口的,必須通過用戶交互才能打開
config.preferences.javaScriptCanOpenWindowsAutomatically =YES;
// 通過js與webview內(nèi)容交互配置
config.userContentController=[[WKUserContentController alloc]init];
// 添加一個(gè)名稱,在JS通過這個(gè)名稱發(fā)送消息
[config.userContentController addScriptMessageHandler:self name:@"GetTKL"];
self.wkwebView = [[WKWebView alloc] initWithFrame:self.webviewSupView.bounds configuration:config];
self.wkwebView.navigationDelegate=self;
self.wkwebView.UIDelegate=self;
//允許右滑返回上個(gè)鏈接,左滑前進(jìn)
self.wkwebView.allowsBackForwardNavigationGestures = YES;
//允許鏈接3D Touch
self.wkwebView.allowsLinkPreview = YES;
[self.wkwebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[ZebUserInfo sharedInstance].userShoppingMallUrl]]];
[self.webviewSupView addSubview:self.wkwebView];
[self.wkwebView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(0);
make.top.mas_equalTo(0);
make.right.mas_equalTo(0);
make.bottom.mas_equalTo(0);
}];
}
#pragma mark UIProgressView初始化
-(void)setUIProgressView{
//進(jìn)度條初始化
self.progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 0, KDeviceWidth, 2)];
self.progressView.backgroundColor = [UIColor whiteColor];
self.progressView.progressTintColor = [FSColor mainColor];
self.progressView.trackTintColor = [UIColor clearColor];
//設(shè)置進(jìn)度條的高度,下面這句代碼表示進(jìn)度條的寬度變?yōu)樵瓉淼?倍,高度變?yōu)樵瓉淼?.5倍.
self.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.5f);
[self.webviewSupView addSubview:self.progressView];
@weakify(self)
[RACObserve(self.wkwebView, estimatedProgress) subscribeNext:^(id x) {
self_weak_.progressView.progress = self_weak_.wkwebView.estimatedProgress;
if (self_weak_.progressView.progress == 1) {
/*
*添加一個(gè)簡單的動(dòng)畫,將progressView的Height變?yōu)?.4倍,在開始加載網(wǎng)頁的代理中會(huì)恢復(fù)為1.5倍
*動(dòng)畫時(shí)長0.25s,延時(shí)0.3s后開始動(dòng)畫
*動(dòng)畫結(jié)束后將progressView隱藏
*/
[UIView animateWithDuration:0.25f delay:0.3f options:UIViewAnimationOptionCurveEaseOut animations:^{
self_weak_.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.4f);
} completion:^(BOOL finished) {
self_weak_.progressView.hidden = YES;
}];
}}];
}
#pragma mark 代理
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
//此處可以得到之前配置項(xiàng)注入的名稱,并且可以攜帶數(shù)據(jù),json對象或者json字串等~
if ([message.name isEqualToString:@"GetTKL"]) {
NSDictionary *dict=message.body;
NSArray *Arr=dict[@"data"];
ZEBLog(@"js調(diào)用oc---------begin--------");
if (Arr.count>0) {
dispatch_sync(dispatch_get_main_queue(), ^{
[MBProgressHUD showSuccess:@"復(fù)制成功!"];
});
}
[ZebUserInfo sharedInstance].shareDocument=[NSString stringWithFormat:@"%@",Arr[0]];
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = [ZebUserInfo sharedInstance].shareDocument;
ZEBLog(@"js調(diào)用oc---------The End-------");
}
}
//在發(fā)送請求之前,決定是否跳轉(zhuǎn)
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
ZEBLog(@"在發(fā)送請求之前,決定是否跳轉(zhuǎn):%@",navigationAction.request.URL);
self.currentURL = webView.URL.absoluteString;
decisionHandler(WKNavigationActionPolicyAllow);
}
//開始加載
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
ZEBLog(@"開始加載網(wǎng)頁");
//開始加載網(wǎng)頁時(shí)展示出progressView
self.progressView.hidden = NO;
//開始加載網(wǎng)頁的時(shí)候?qū)rogressView的Height恢復(fù)為1.5倍
self.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.5f);
//防止progressView被網(wǎng)頁擋住
[self.view bringSubviewToFront:self.progressView];
}
//在收到響應(yīng)后,決定是否跳轉(zhuǎn)
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {
ZEBLog(@"在收到響應(yīng)后,決定是否跳轉(zhuǎn)");
decisionHandler(WKNavigationResponsePolicyAllow);
}
// 當(dāng)內(nèi)容開始返回時(shí)調(diào)用
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
ZEBLog(@"內(nèi)容開始返回");
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
ZEBLog(@"加載完成");
//加載完成后隱藏progressView
self.progressView.hidden = YES;
}
//加載失敗
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
ZEBLog(@"加載失敗");
//加載失敗同樣需要隱藏progressView
self.progressView.hidden = YES;
}
//接收到服務(wù)器跳轉(zhuǎn)請求之后調(diào)用
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation {
ZEBLog(@"接收到服務(wù)器跳轉(zhuǎn)請求之后調(diào)用");
}
- (void)dealloc {
// VC銷毀時(shí),移除交互對象
[self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"GetTKL"];
}
@end