0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

WKWebViewのWKNavigationDelegateについて

0
Last updated at Posted at 2023-04-04

WKWebViewについて自分なりに整理しておきたかったので投稿です。
ダウンロード進行管理してくれるnavigationResponsenavigationActionは初めて知りました。

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でダウンロード開始 -----")
    }

その他

気が向けばなにか追記します。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?