[iOS] WKNavigationDelegate 메서드 알아보기

웹 뷰 로딩.. 어떤 순서로 실행되고 있던걸까요?

들어가기 전에

  1. 사용자의 링크 클릭 또는 URL 입력과 같은 유저의 Action 으로 시작됩니다. 이를 initial request, 혹은 navigation start 라고 부릅니다.
  2. 이후 서버에서 처리를 마치고 네트워크를 통해 reponse 를 사용자의 브라우저로 다시 전송합니다. 이를 response start 혹은 first byte 라고 부릅니다.
  3. 사용자의 브라우저가 response 를 수신하기 시작하고, Document Object Model 또는 DOM 처리를 시작합니다.
  4. DOM 로드가 완료되는 시점을 DOM ready 라고 하며, DOM을 사용하여 사용자의 브라우저가 페이지를 렌더링하기 시작합니다.
  1. Action / Request — 사용자의 URL 입력 혹은 링크 클릭 등으로 발생하는 웹 페이지에 대한 Client 의 요청
  2. Response — Server의 응답
decidePolicyFor navigationAction: WKNavigationAction
decidePolicyFor navigationResponse: WKNavigationResponse
  • 첫번째는 navigationAction 에 대한 policy 를 정한다고? 음.. 처음 요청할때에 대한 policy 를 설정하는거겠군?
  • 두번째는 navigationResponse 에 대한 policy 를 정한다고? 음… 서버에서 응답을 받고나서 policy 를 설정하는거겠군?
  1. func webView(WKWebView, decidePolicyFor: WKNavigationAction, preferences: WKWebpagePreferences, decisionHandler: (WKNavigationActionPolicy, WKWebpagePreferences) -> Void)
  2. func webView(WKWebView, didStartProvisionalNavigation: WKNavigation!)
  3. func webView(WKWebView, decidePolicyFor: WKNavigationResponse, decisionHandler: (WKNavigationResponsePolicy) -> Void)
  4. func webView(WKWebView, didCommit: WKNavigation!)
  5. func webView(WKWebView, didFinish: WKNavigation!)

1. navigation request 를 허용 / 거절하기

webView(_:decidePolicyFor:decisionHandler:)

optional func webView(
_ webView: WKWebView,
decidePolicyFor navigationAction: WKNavigationAction,
decisionHandler: @escaping (WKNavigationActionPolicy) -> Void
)
decisionHandler(.allow)
decisionHandler(.cancel)
decisionHandler(.download)

webView(_:decidePolicyFor:preferences:decisionHandler:)

optional func webView(
_ webView: WKWebView,
decidePolicyFor navigationAction: WKNavigationAction,
preferences: WKWebpagePreferences,
decisionHandler: @escaping (WKNavigationActionPolicy, WKWebpagePreferences) -> Void
)

webView(_:decidePolicyFor:decisionHandler:)

optional func webView(
_ webView: WKWebView,
decidePolicyFor navigationResponse: WKNavigationResponse,
decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void
)

2. request 의 로딩 진행률 추적하기

webView(_:didStartProvisionalNavigation:)

optional func webView(
_ webView: WKWebView,
didStartProvisionalNavigation navigation: WKNavigation!
)

webView(_:didReceiveServerRedirectForProvisionalNavigation:)

optional func webView(
_ webView: WKWebView,
didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!
)

webView(_:didCommit:)

optional func webView(
_ webView: WKWebView,
didCommit navigation: WKNavigation!
)

webView(_:didFinish:)

optional func webView(
_ webView: WKWebView,
didFinish navigation: WKNavigation!
)

3. 인증(authentication) 문제 대응하기

webView(_:didReceive:completionHandler:)

optional func webView(
_ webView: WKWebView,
didReceive challenge: URLAuthenticationChallenge,
completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void
)

webView(_:authenticationChallenge:shouldAllowDeprecatedTLS:)

optional func webView(
_ webView: WKWebView,
authenticationChallenge challenge: URLAuthenticationChallenge,
shouldAllowDeprecatedTLS decisionHandler: @escaping (Bool) -> Void
)

4. navigation 에러 대응하기

webView(_:didFailProvisionalNavigation:withError:)

optional func webView(
_ webView: WKWebView,
didFail navigation: WKNavigation!,
withError error: Error
)

webView(_:didFail:withError:)

optional func webView(
_ webView: WKWebView,
didFailProvisionalNavigation navigation: WKNavigation!,
withError error: Error
)

webViewWebContentProcessDidTerminate(_:)

optional func webViewWebContentProcessDidTerminate(_ webView: WKWebView)

5. 다운로드 진행률 처리하기

webView(_:navigationAction:didBecome:)

optional func webView(
_ webView: WKWebView,
navigationAction: WKNavigationAction,
didBecome download: WKDownload
)

webView(_:navigationResponse:didBecome:)

optional func webView(
_ webView: WKWebView,
navigationResponse: WKNavigationResponse,
didBecome download: WKDownload
)

마무리

  1. func webView(WKWebView, decidePolicyFor: WKNavigationAction, preferences: WKWebpagePreferences, decisionHandler: (WKNavigationActionPolicy, WKWebpagePreferences) -> Void)

--

--

 https://github.com/sujinnaljin/TIL

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store