初识WKWebView

参考文档1
参考文档2
参考文档3

微信 iOS 客户端将于3月1日前逐步升级为WKWebview内核,2014年推出的WKWebView将替代UIKit 中笨重,内存泄漏的UIWebView,其拥有滚动刷新率与safari下相同的JavaScript 引擎优势。

常用API
//webview配置
@property (nonatomic, readonly, copy) WKWebViewConfiguration *configuration;
//页面标题,支持KVO
@property (nullable, nonatomic, readonly, copy) NSString *title;
//当前请求的URL,支持KVO
@property (nullable, nonatomic, readonly, copy) NSURL *URL;
//标识当前是否正在加载内容中,支持KVO
@property (nonatomic, readonly, getter=isLoading) BOOL loading;

// 历史访问列表;
@property (nonatomic, readonly, strong) WKBackForwardList *backForwardList;

//标识是否支持左右swipe手势,是否可以前进后退,默认NO
@property (nonatomic) BOOL allowsBackForwardNavigationGestures;
//当前加载进度【0,1】
@property (nonatomic, readonly) double estimatedProgress;
//标识页面中所有资源是否台安县过安全加密连接来加载,支持KVO
@property (nonatomic, readonly) BOOL hasOnlySecureContent;

//是否可以执行goback操作,支持KVO
@property (nonatomic, readonly) BOOL canGoBack;
//是否可以执行gofarward操作,支持KVo
@property (nonatomic, readonly) BOOL canGoForward;
//返回上一页(如果不能,则什么都不做)
- (nullable WKNavigation *)goBack;
//进入下一页(如果不能,则什么都不做)
- (nullable WKNavigation *)goForward;


//重新载入页面
- (nullable WKNavigation *)reload;
//重新载入原始页面
- (nullable WKNavigation *)reloadFromOrigin;
//停止加载数据
- (void)stopLoading;
//执行JS 代码
- (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^ _Nullable)(_Nullable id, NSError * _Nullable error))completionHandler;

JavaScript配置相关
//所有添加的WKUserScript都在这里
@property (nonatomic, readonly, copy) NSArray<WKUserScript *> *userScripts;
//注入JS
- (void)addUserScript:(WKUserScript *)userScript;
//移除所有注入的JS
- (void)removeAllUserScripts;

//JS调用原生方法可能过下面方法
//添加scriptMessageHandler到所有frames中,都可以通过
//window.webkit.messageHandlers.<name>.postMessage(<messagebody>)
- (void)addScriptMessageHandler:(id <WKScriptMessageHandler>)scriptMessageHandler name:(NSString *)name;
//根据name移除所有注入的scriptMessageHandler
- (void)removeScriptMessageHandlerForName:(NSString *)name;
加载相关
//从URLRequest加载
- (nullable WKNavigation *)loadRequest:(NSURLRequest *)request;
//从本地加载,仅限iOS 9以上
- (nullable WKNavigation *)loadFileURL:(NSURL *)URL allowingReadAccessToURL:(NSURL *)readAccessURL API_AVAILABLE(macosx(10.11), ios(9.0));
//从HTML字符串加载
- (nullable WKNavigation *)loadHTMLString:(NSString *)string baseURL:(nullable NSURL *)baseURL;
//从二进制流中加载
- (nullable WKNavigation *)loadData:(NSData *)data MIMEType:(NSString *)MIMEType characterEncodingName:(NSString *)characterEncodingName baseURL:(NSURL *)baseURL API_AVAILABLE(macosx(10.11), ios(9.0));
代理相关
WKNavigationDelegate
//在发送请求前,决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler;
//收到服务器的响应,根据response相关信息,决定是否跳转,
//decisionHandler必须调用以决定是否跳转。
//WKNavigationResponsePolicyCancel-取消跳转
//WKNavigationResponsePolicyAllow-允许跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler;

//准备加载页面 == shouldStartLoadWithRequest
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation;
//开始获取页面内容
- (void)webView:(WKWebView *)webView didCommitNavigation:(null_unspecified WKNavigation *)navigation;

//页面加载完成== didFinishLoad
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation;
//页面内容加载完成,iOS 9
- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView API_AVAILABLE(macosx(10.11), ios(9.0));

//页面跳转失败 == didFailLoadWithError
- (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error;
//页面加载失败
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error;
WKUIDelegate
// 创建新的WebView;
- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures {}
// 拦截警告框
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler {}
// 拦截确认框
- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL))completionHandler {}

基本使用
@interface ViewController ()<WKNavigationDelegate,WKScriptMessageHandler>
@property(nonatomic,retain)WKWebView *webView;
@property (nonatomic,retain)UIProgressView  *progressView;
@property (nonatomic,retain)WKUserContentController *userContentController;
@end

-(WKWebView*)webView {
    if (!_webView) {
        
        //先实例化配置类 以前UIWebView的属性有的放到了这里
        WKWebViewConfiguration * configuration = [[WKWebViewConfiguration alloc]init];
       
        //注册供js调用的方法
        _userContentController =[[WKUserContentController alloc]init];
        //弹出登录
        [_userContentController addScriptMessageHandler:self  name:@"loginVC"];
        //进入详情页
        [_userContentController addScriptMessageHandler:self  name:@"gotodetailVC"];
        
        configuration.userContentController = _userContentController;
        configuration.preferences.javaScriptEnabled = YES;//打开js交互

        //创建webView
        _webView = [[WKWebView alloc]initWithFrame:[UIScreen mainScreen].bounds configuration:configuration];
        _webView.navigationDelegate = self;
        
        _webView.allowsLinkPreview = YES;//允许预览链接
        _webView.allowsBackForwardNavigationGestures = YES;//允许滑动返回
        
        //创建进度条,WkWebView自带预估进度
        _progressView = [[UIProgressView alloc]initWithFrame:CGRectMake(0.5, 0, _webView.frame.size.width, 2)];
        _progressView.tintColor = [UIColor blueColor];
        _progressView.trackTintColor = [UIColor whiteColor];
        [_webView addSubview:_progressView];
        //添加观察者拿到进度
        [_webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
        
        //创建请求
        NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"];
        //可变的请求可以设置缓存
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
        request.cachePolicy = NSURLRequestReturnCacheDataElseLoad;

        //加载
        [_webView loadRequest:request];
    }
    return _webView;
}

//设置进度条
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    
    if ([keyPath isEqualToString:@"estimatedProgress"]) {
        _progressView.hidden = NO;
        CGFloat progress = [change[@"new"] floatValue];
        [_progressView setProgress:progress];
        if (progress == 1.0) {
            _progressView.hidden = YES;
        }
    }
    
}

JS 与 OC 的交互

  1. JS 调用OC 方法时可以发送请求,
    或通过WKUIDelegate(alert 和confirm),OC 进行拦截区分请求或弹窗实现交互。
  2. 通过WKScriptMessageHandler 参考文档
JS 调用 oc 方法
JS写法
window.webkit.messageHandlers.<name>.postMessage(<messageBody>)
//其中<name>,就是上面方法里的第二个参数`name`。
//例如我们调用API的时候第二个参数填@"Share",那么在JS里就是:
//window.webkit.messageHandlers.Share.postMessage(<messageBody>)
//<messageBody>是一个键值对,键是body,值可以有多种类型的参数。
// 在`WKScriptMessageHandler`协议中,我们可以看到mssage是`WKScriptMessage`类型,有一个属性叫body。
// 而注释里写明了body 的类型:
Allowed types are NSNumber, NSString, NSDate, NSArray, NSDictionary, and NSNull.
//JS调用OC方法
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{

    if(message.name ==nil || [message.name isEqualToString:@""])
        return;
    //message body : js 传过来值
    NSLog(@"message.body ==%@",message.body);

    if ([message.name isEqualToString:@"loginVC"])
    {
        [self showLoginView];
    }
    else if ([message.name isEqualToString:@"gotodetailVC"])
    {
       //进入详情页
        NSString*url = [message.body objectForKey:@"body"];
        [self gotodetial:url];
    }

}
oc 调用JS 方法
//OC调用JS方法,给JS传值
//加载完成后通过evaluateJavaScript实现传值
    [webView evaluateJavaScript:@"document.getElementById(\"content\").offsetHeight;" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
        //获取页面高度,并重置webview的frame
        CGFloat documentHeight = [result doubleValue];
        CGRect frame = webView.frame;
        frame.size.height = documentHeight+[UIScreen mainScreen].bounds.size.height-58 /*显示不全*/;
        webView.frame = frame;
    }];
    
//    NSString *js = [NSString stringWithFormat:@"isLogin(%d)",[[UserInfoSingleton shareUserInfoSingleton] isLogin]];
//    [webView evaluateJavaScript:js completionHandler:^(id item, NSError * _Nullable error) {
//    }];
   
//    NSString *js1 = [NSString stringWithFormat:@"userId(\'%@\')",[UserInfoSingleton shareUserInfoSingleton].userId];
//    [webView evaluateJavaScript:js1 completionHandler:^(id item, NSError * _Nullable error) {
//        
//    }];    
  1. WebViewJavascriptBridge参考文档
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269

推荐阅读更多精彩内容