Skip to content

Instantly share code, notes, and snippets.

@ElonPark
Last active November 20, 2023 09:28
Show Gist options
  • Save ElonPark/e26cd20ebb8c8d66b56a0b99449ca081 to your computer and use it in GitHub Desktop.
Save ElonPark/e26cd20ebb8c8d66b56a0b99449ca081 to your computer and use it in GitHub Desktop.
WKWebView에서 자바스크립트로 window.open(), window.close() 하는 경우 처리
/** - 새창 열기, 닫기 예제 **/
///메인으로 사용중인 웹뷰
lazy var webView = WKWebView()
///window.open()으로 열리는 새창
var createWebView: WKWebView?
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!
/* 현재 창에서 열고 싶은 경우
self.webView.load(navigationAction.request)
return nil
*/
}
///새창 닫기
///iOS9.0 이상
func webViewDidClose(_ webView: WKWebView) {
if webView == createWebView {
createWebView?.removeFromSuperview()
createWebView = nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment