1
0

More than 3 years have passed since last update.

UIWebViewでよく使うデリゲートメソッド一覧(Swift)

Last updated at Posted at 2020-11-06

UIWebViewでよく使うデリゲートメソッド一覧

webView(_:shouldStartLoadWith:navigationType:)

概要
ロードの開始前に呼ばれる
ロードするかどうか
false を返すとロードしない

用途
特定のURLの場合にロードしないようにする、など

FooViewController.swift
import UIKit

final class FooViewController: UIViewController, UIWebViewDelegate {

    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebView.NavigationType) -> Bool {
        if let url = request.url?.absoluteString, url.lowercased() == "https://example.com".lowercased() {
            return false
        }
        return true
    }

}

webViewDidFinishLoad(_:)

概要
ロードの完了後に呼ばれる

用途
インジケータを消す場合、など

FooViewController.swift
import UIKit

final class FooViewController: UIViewController, UIWebViewDelegate {

    @IBOutlet private weak var indicatorView: UIActivityIndicatorView!

    func webViewDidFinishLoad(_ webView: UIWebView) {
        self.indicatorView.stopAnimating()
    }

}

webView(_:didFailLoadWithError:)

概要
ロード時にエラーが発生した場合に呼ばれる

用途
エラーハンドリング
インジケータを消す場合、など

FooViewController.swift
import UIKit

final class FooViewController: UIViewController, UIWebViewDelegate {

    @IBOutlet private weak var indicatorView: UIActivityIndicatorView!

    func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {
        self.indicatorView.stopAnimating()
    }

}
1
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
1
0