본문 바로가기
IOS

IOS Webview 하이브리드 Alert처리

by 일용직 코딩노동자 2020. 6. 8.
728x90
반응형

IOS swift에서 얼럿(Alert)창 알림창을 처리하는 코드입니다.

 

    func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo,
                 completionHandler: @escaping () -> Void) {
        let completionHandlerWrapper = CompletionHandlerWrapper(completionHandler: completionHandler, defaultValue: Void())
        
        let alertController = UIAlertController(title: "", message: message, preferredStyle: .alert)
        alertController.addAction(UIAlertAction(title: "확인", style: .default, handler: { (action) in
            completionHandlerWrapper.respondHandler(Void())
        }))
        
        self.present(alertController, animated: true, completion: nil)
    }
    
    func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo,
                 completionHandler: @escaping (Bool) -> Void) {
        
        let completionHandlerWrapper = CompletionHandlerWrapper(completionHandler: completionHandler, defaultValue: false)
        
        let alertController = UIAlertController(title: "", message: message, preferredStyle: .alert)
        alertController.addAction(UIAlertAction(title: "확인", style: .default, handler: { (action) in
            completionHandlerWrapper.respondHandler(true)
        }))
        
        alertController.addAction(UIAlertAction(title: "취소", style: .default, handler: { (action) in
            completionHandlerWrapper.respondHandler(false)
        }))
        
        self.present(alertController, animated: true, completion: nil)
    }
    
    func webView(_ webView: WKWebView, runJavaScriptTextInputPanelWithPrompt prompt: String, defaultText: String?, initiatedByFrame frame: WKFrameInfo,
                 completionHandler: @escaping (String?) -> Void) {
        let completionHandlerWrapper = CompletionHandlerWrapper(completionHandler: completionHandler, defaultValue: "")
        let alertController = UIAlertController(title: "", message: prompt, preferredStyle: .alert)
        alertController.addTextField { (textField) in
            textField.text = defaultText
        }
        
        alertController.addAction(UIAlertAction(title: "확인", style: .default, handler: { (action) in
            if let text = alertController.textFields?.first?.text {
                completionHandlerWrapper.respondHandler(text)
            } else {
                completionHandlerWrapper.respondHandler(defaultText)
            }
        }))
        
        alertController.addAction(UIAlertAction(title: "취소", style: .default, handler: { (action) in
            completionHandlerWrapper.respondHandler(nil)
        }))
        
        self.present(alertController, animated: true, completion: nil)
    }

 

class CompletionHandlerWrapper<Element> {
    private var completionHandler: ((Element) -> Void)?
    private let defaultValue: Element
    
    init(completionHandler: @escaping ((Element) -> Void), defaultValue: Element) {
        self.completionHandler = completionHandler
        self.defaultValue = defaultValue
    }
    
    func respondHandler(_ value: Element) {
        completionHandler?(value)
        completionHandler = nil
    }
    
    deinit {
        respondHandler(defaultValue)
    }
}

하나하나 이해하고 넘어가기엔 너무 귀찮은 코드이기도 합니다.

 

수정하실건 확인버튼과 취소버튼 일것같습니다.

 

확인 대신 OK를 넣기도 합니다,

728x90
반응형

댓글