diff --git a/demos/mobile/ios/Blockly WebView/ViewController.swift b/demos/mobile/ios/Blockly WebView/ViewController.swift index 6a8f2418d..dee718c07 100644 --- a/demos/mobile/ios/Blockly WebView/ViewController.swift +++ b/demos/mobile/ios/Blockly WebView/ViewController.swift @@ -9,7 +9,7 @@ import UIKit import WebKit -class ViewController: UIViewController { +class ViewController: UIViewController, WKUIDelegate { /// The name used to reference this iOS object when executing callbacks from the JS code. /// If this value is changed, it should also be changed in the `CODE_GENERATOR_BRIDGE_JS` file. fileprivate static let HOST_HTML = "Blockly/webview.html" @@ -18,21 +18,60 @@ class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() + webView.uiDelegate = self // Do any additional setup after loading the view, typically from a nib. - loadWebView() + loadWebContent() } - func loadWebView() { + func loadWebContent() { if let htmlUrl = Bundle.main.url(forResource: "webview", withExtension: "html", subdirectory: "Blockly") { webView.load(URLRequest.init(url: htmlUrl)) } } + func webView(_ webView: WKWebView, + runJavaScriptAlertPanelWithMessage message: String, + initiatedByFrame frame: WKFrameInfo, + completionHandler: @escaping () -> Void) { + + let alert = UIAlertController(title: nil, message: message, preferredStyle: .alert) + let title = NSLocalizedString("OK", comment: "OK Button") + let ok = UIAlertAction(title: title, style: .default) { (action: UIAlertAction) -> Void in + alert.dismiss(animated: true, completion: nil) + } + alert.addAction(ok) + present(alert, animated: true) + completionHandler() + } + + func webView(_ webView: WKWebView, + runJavaScriptConfirmPanelWithMessage message: String, + initiatedByFrame frame: WKFrameInfo, + completionHandler: @escaping (Bool) -> Void) { + + let alert = UIAlertController(title: nil, message: message, preferredStyle: .alert) + let closeAndHandle = { (okayed: Bool) in + alert.dismiss(animated: true, completion: nil) + completionHandler(okayed) + } + + let okTitle = NSLocalizedString("OK", comment: "OK button title") + let ok = UIAlertAction(title: okTitle, style: .default) { (action: UIAlertAction) -> Void in + closeAndHandle(true) + } + alert.addAction(ok) + + let cancelTitle = NSLocalizedString("Cancel", comment: "Cancel button title") + let cancel = UIAlertAction(title: cancelTitle, style: .default) { (action: UIAlertAction) -> Void in + closeAndHandle(false) + } + alert.addAction(cancel) + present(alert, animated: true) + } + override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } - - }