• WKWebView简单使用


     最近没事看了一下 WKWebView ,就把自己总结的记录一下,方便查看.

    注:一下为个人总结,会有不足,热烈欢迎大神指点

    1> 创建
    导入:#import <WebKit/WebKit.h>
    WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com"]]];
    [self.view addSubview:webView];

    2> WKWebView的代理有三个 
    WKUIDelegate,WKNavigationDelegate,WKScriptMessageHandler

    3> 代理方法
    3.1> WKUIDelegate的代理方法
    /** 创建新的webView 指定配置对象、导航动作对象、window特性 */
    -(WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures{}
    /** 显示一个与JS交互的确认Alert(弹框) */
    - (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler{}
    /** 弹出一个输入框(与JS交互的) */
    - (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(nullable NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * _Nullable result))completionHandler{}
    /** 显示一个与JS交互的警告Alert(弹框) */
    - (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler{}
    /** webview关闭时的回调 */
    - (void)webViewDidClose:(WKWebView *)webView{}
    3.2> WKNavigationDelegate代理方法
    #pragma mark 导航监听
    /** 在发送请求之前,决定是否跳转 */
    - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{}
    /** 收到身份验证 */
    - (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler{}
    /** 在收到响应后,决定是否跳转 */
    - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{}
    /** 接收到服务器跳转请求之后再执行 */
    - (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(null_unspecified WKNavigation *)navigation{}
    /** 页面加载失败 */
    - (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error{}
    /** WKWebView终止 */
    - (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView{}

    #pragma mark 网页监听
    /** 页面开始加载 */
    - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation{}
    /** 当内容开始返回时调用 */
    - (void)webView:(WKWebView *)webView didCommitNavigation:(null_unspecified WKNavigation *)navigation{}
    /** 页面加载完成之后调用 */
    - (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{}
    /** 页面加载失败时调用 */
    - (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error{}

    3.3> WKScriptMessageHandler代理方法
    /** 网页通过JS把消息发送给OC */
    - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{}

  • 相关阅读:
    JS-两个空数组为什么不相等?
    ES6---箭头函数()=>{} 与function的区别(转载)
    SASS用法指南
    scss/less语法以及在vue项目中的使用(转载)
    基于vue+mint-ui的mobile-h5的项目说明
    vue中mint-ui的filed的与blur事件结合实现检查用户输入是否正确
    Carrierwave 如何配置合理的上传文件名(转自李华顺)
    ruby大神与菜鸟的代码区别
    用imageMagick合成图片添加图片水印
    想做喜欢的安卓应用
  • 原文地址:https://www.cnblogs.com/xuan-yuan/p/6897679.html
Copyright © 2020-2023  润新知