본문 바로가기
모바일/IOS

WKWebview에서 자바스크립트 호출

by 죠부니 2018. 4. 24.
반응형

일반웹뷰라면 밑에 링크가 맞는데

https://stackoverflow.com/questions/36139062/call-swift-function-with-javascript-using-uiwebview

WKWebView에서는 변화사항이 있는거같다.

https://stackoverflow.com/questions/40761218/javascript-call-to-swift-from-uiwebview

사용된정보

https://styleshare.github.io/2016/09/14/ios-10-url-open.html

https://samwize.com/2016/06/08/complete-guide-to-implementing-wkwebview/

https://gist.github.com/starhoshi/efde2a0283f05e6a4d32a225617294ab

결론

WKNavigationDelete추가

    override func viewDidLoad() {

        super.viewDidLoad()

        WebView.navigationDelegate = self

}


HTML페이지에는

location.href = "custom-scheme//test"



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

        if let url = navigationAction.request.url, url.scheme != "http" && url.scheme != "https" {

            print(url.scheme)

            print(url.host)

            //UIApplication.shared.openURL(url)

            decisionHandler(.cancel)

        } else {

            decisionHandler(.allow)

        }

    }



PRINT결과

Optional("custom-scheme")

Optional("test")



반응형