Slide 156
Slide 156 text
ೖྗμΠΞϩά - runJavaScriptTextInputPanelWithPrompt -
// MARK: - WKUIDelegate
extension ViewController: WKUIDelegate {
// λΠϛϯά: JavaScript͔Βprompt()͕ݺΕͨͱ͖
func webView(
_ webView: WKWebView,
runJavaScriptTextInputPanelWithPrompt prompt: String,
defaultText: String?,
initiatedByFrame frame: WKFrameInfo,
completionHandler: @escaping (String?) -> Void
) {
let alert = UIAlertController(title: prompt, message: nil, preferredStyle: .alert)
alert.addTextField { textField in
textField.text = defaultText
}
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel) { _ in
completionHandler(nil)
})
alert.addAction(UIAlertAction(title: "OK", style: .default) { _ in
completionHandler(alert.textFields?.first?.text)
})
webView.window?.rootViewController?.present(alert, animated: true, completion: nil)
}