투케이2K

346. (ios/swift5) WKWebview 웹뷰 자바스크립트 Prompt 팝업창 호출 이벤트 처리 본문

IOS

346. (ios/swift5) WKWebview 웹뷰 자바스크립트 Prompt 팝업창 호출 이벤트 처리

투케이2K 2023. 10. 11. 20:44

[개발 환경 설정]

개발 툴 : XCODE

개발 언어 : SWIFT5

 

[소스 코드]

 

    // -----------------------------------------------------------------------------------------
    // MARK: - [웹뷰 Prompt 팝업창 처리]
    // -----------------------------------------------------------------------------------------
    func webView(_ webView: WKWebView, runJavaScriptTextInputPanelWithPrompt prompt: String, defaultText: String?, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (String?) -> Void) {
        S_Log._D_(description: "웹뷰 Prompt 팝업창 호출 표시", data: [ "message :: \(prompt)" ])

        // -----------------------------------------
        // [alert 팝업창 호출]
        // -----------------------------------------
        let alertController = UIAlertController(title: "[Web-P] : 알림", message: prompt, preferredStyle: .alert)
        alertController.addTextField { (textField) in
            textField.text = defaultText ?? ""
        }
        alertController.addAction(UIAlertAction(title: "취소", style: .cancel, handler: { (action) in completionHandler("") }))
        alertController.addAction(UIAlertAction(title: "확인", style: .default, handler: { (action) in
            if let text = alertController.textFields?.first?.text {
                completionHandler(text)
            } else {
                completionHandler("")
            }
        }))
        self.present(alertController, animated: true, completion: nil)
        // -----------------------------------------
    }

 

반응형
Comments