WKWebViewについて自分なりに整理しておきたかったので投稿です。
ダウンロード進行管理してくれるnavigationResponseとnavigationActionは初めて知りました。
WKWebView
Apple Developer WKNavigationデリゲート
一言メモ:WKWebViewに対してプロトコルのメソッドを実装することで、WebViewのリクエスト・エラー・ダウンロードなどを追跡(検知)できる。
実装
WKNavigationDelegate - Allowing or denying navigation requests
// MARK: - WKNavigationDelegate - Allowing or denying navigation requests
/// WebViewが新しいページを読み込もうとしているとき呼び出され、読み込み許可・拒否をwebViewに通知する
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, preferences: WKWebpagePreferences, decisionHandler: @escaping (WKNavigationActionPolicy, WKWebpagePreferences) -> Void) {
print("----- WebViewの読み込み許可・拒否を決定 -----")
// Webページの読み込みを許可する(呼び出さないとWebViewの読み込みが進まなくなる)
decisionHandler(.allow, preferences)
}
WKNavigationDelegate - Tracking the load progress of a request
// MARK: - WKNavigationDelegate - Tracking the load progress of a request
/// WebViewの読み込み開始時に呼び出される
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
print("----- WebViewの読み込み開始 -----")
}
/// リダイレクト開始時に呼び出される
func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
print("----- WebViewリダイレクト開始 -----")
}
/// WebViewのウェブコンテンツの読み込みが完了し、ページの表示が開始されたときに呼び出される
func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
print("----- WebViewのコンテンツ読み込み完了、ページ表示が開始 -----")
}
/// WebViewの読み込み完了時に呼び出される
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
print("----- WebView読み込みが完了 -----")
}
WKNavigationDelegate - Responding to navigation errors
// MARK: - WKNavigationDelegate - Responding to navigation errors
/// WebViewの読み込みが開始されてからエラーが発生したとき、またはページの読み込みが中止されたときに呼び出される
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
print("----- WebView読み込みが中止 -----")
}
/// WebViewの読み込みが完了した後に失敗したときに呼び出される
/// ネットワークに接続できない場合、無効なURLにアクセスしようとした場合、または読み込みに必要なリソースが見つからない場合に発生
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
print("----- WebView読み込み完了後にエラーが発生 -----")
}
WKNavigationDelegate - Handling download progress
// MARK: - WKNavigationDelegate - Handling download progress
@available(iOS 14.5, *)
/// Tells the delegate that a navigation response became a download.
/// - Parameters:
/// - webView: The web view in which the navigation response took place.
/// - navigationResponse: Descriptive information about the navigation response that turned into a download.
/// - download: An object that represents the download of a web resource.
func webView(_ webView: WKWebView, navigationResponse: WKNavigationResponse, didBecome download: WKDownload) {
print("----- WebViewでダウンロード準備 -----")
}
@available(iOS 14.5, *)
/// Tells the delegate that a navigation action became a download.
/// - Parameters:
/// - webView: The web view in which the navigation action took place.
/// - navigationAction: Descriptive information about the navigation response that turned into a download.
/// - download: An object that represents the download of a web resource.
func webView(_ webView: WKWebView, navigationAction: WKNavigationAction, didBecome download: WKDownload) {
print("----- WebViewでダウンロード開始 -----")
}
その他
気が向けばなにか追記します。