LoginSignup
36
36

More than 5 years have passed since last update.

【iOS】【Objective-C】WKWebView切り替えた時のメモ(WKNavigationDelegate編)

Last updated at Posted at 2016-02-07

UIWebView→WKWebViewをちょっと試してみたのでその時のメモを。

(最終的にはNSURLProtocolと組み合わせができないということで切り替え自体は断念)

その前に、

WKWebViewは、WebKitフレームワークの追加が必要。
またstoryboardなどからの追加はできない。


#import <WebKit/WebKit.h>

まずはdelegate編

WKNavigationDelegateとWKUIDelegate

WKWebViewにおいては、delegateが二種類用意されている。
主に、ロードに関するものがWKNavigationDelegateで、
WKUIDelegateについては、ユーザーからの動き(js)との連携が主というところ。

今回はWKNavigationDelegateの一部を。

shouldStartLoadWithRequest

UIWebViewにおける、ページ遷移の前に実行されるdelegateメソッド
ページ遷移するかどうかを決定することができる。
return no
をすることで、ページ遷移させないことができる。

WKWebViewにおいては


- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{

    //NSURLの取得
    NSURL *url= [webView URL];
    //NSURLRequestの取得
    NSURLRequest *req = [navigationAction request];

    //キャンセル
    //decisionHandler(WKNavigationActionPolicyCancel);
    //読み込み続行
    decisionHandler(WKNavigationActionPolicyAllow);

}


webViewDidStartLoad

読み込み開始時に実行されるUIWebViewのdelegateメソッド

WKWebViewにおいては。


- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation
{
}

webViewDidFinishLoad

読み込み完了時に実行されるUIWebViewのdelegateメソッド


- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{

}

didFailLoadWithError

読み込み時においてエラーが発生した場合のメソッド

- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error
{

}

36
36
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
36
36