mirror of
https://github.com/google/blockly.git
synced 2026-01-04 15:40:08 +01:00
Add handlers for console.alert and console.confirm.
This commit is contained in:
@@ -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.
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user