iOS WKWebView学习

WKWebView是iOS8.0出来的,用于替代UIWebView的,苹果建议iOS8.0之后使用WKWebView。官方文档示例代码如下:

import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
    
    var webView: WKWebView!
    
    override func loadView() {
        let webConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.uiDelegate = self
        view = webView
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let myURL = URL(string:"https://www.apple.com")
        let myRequest = URLRequest(url: myURL!)
        webView.load(myRequest)
    }}

使用非常简便,主要步骤为创建WKWebView对象,添加到View上,设置代理,创建请求对象,加载请求。
WKWebView主要有两个代理,分别是navigationDelegate、UIDelegate。

@property(nonatomic, weak) id navigationDelegate;
@property(nonatomic, weak) id UIDelegate;

WKNavigationDelegate代理协议如下:

//Initiating the Navigation
//Called when the web view begins to receive web content.
//开始接收web内容的时候调用该方法
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation;

//Called when web content begins to load in a web view.
//webview开始加载web内容的时候调用该方法
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation;


//Responding to Server Actions
//Called when the web view begins to receive web content.
//开始接收web内容的时候调用该方法
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation;

//Called when a web view receives a server redirect.
//当收到服务器重定向的时候调用该方法
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation;


//Authentication Challenges
//Called when the web view needs to respond to an authentication challenge.
//当需要身份验证的时候调用该方法
- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler;

//Reacting to Errors
//Called when an error occurs during navigation.
//当导航过程中发生一个错误时调用该方法
- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error;

//Called when an error occurs while the web view is loading content.
//当在加载内容的时候发生错误的时候会调用该方法
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error;


//Tracking Load Progress
//Called when the navigation is complete.
//当导航完成时调用该方法
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation;

//Called when the web view’s web content process is terminated.
//当webview的内容加载程序终止时调用该方法
- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView;

//Permitting Navigation
//Decides whether to allow or cancel a navigation.
//决定是否允许或者取消跳转导航
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler;

//Decides whether to allow or cancel a navigation after its response is known.
//在知道响应内容后决定是否允许或者取消跳转导航
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler;

更详细内容可以参考:https://www.jianshu.com/p/833448c30d70

你可能感兴趣的:(iOS WKWebView学习)