IOS에서는 window.open()에 대한 처리를 기본적으로 제공하지 않아 별도로 처리를 해주어야 한다.
웹뷰 컨트롤러에 아래와 같은 펑션을 추가하여 현재 뷰 위에 window.open()으로 호출된 페이지를 덧씌운다.
//window.open() 에서 호출되는 펑션
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
//뷰를 생성하는 경우
let frame = UIScreen.main.bounds
//파라미터로 받은 configuration
createWebView = WKWebView(frame: frame, configuration: configuration)
//오토레이아웃 처리
createWebView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
createWebView?.navigationDelegate = self
createWebView?.uiDelegate = self
view.addSubview(createWebView!)
return createWebView!
}
//window.close() 에서 호출되는 펑션
func webViewDidClose(_ webView: WKWebView) {
if webView == createWebView {
createWebView?.removeFromSuperview()
createWebView = nil
}
}
위 코드를 추가한 후에 window.open()이 실행되었는데 위 펑션이 호출되지 않는다면, 웹뷰에서
javaScriptCanOpenWindowsAutomatically 옵션을 사용가능하게 바꾸어줘야 한다.
window.open()이 실행되는 웹뷰에 아래 라인을 추가한다.
webView.configuration.preferences.javaScriptCanOpenWindowsAutomatically = true //이걸 안쓰면 window.open() / window.close() 등 html에서 제공하는 window 관련 이벤트를 수신하지 못함