From 31a723bf32fc4aa3be9b410c437e4528a7186bc3 Mon Sep 17 00:00:00 2001 From: Andrew n marshall Date: Tue, 16 Oct 2018 16:08:30 -0700 Subject: [PATCH 1/3] Add handlers for console.alert and console.confirm. --- .../ios/Blockly WebView/ViewController.swift | 49 +++++++++++++++++-- 1 file changed, 44 insertions(+), 5 deletions(-) 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. } - - } From 4ce4544cc6203edd8821d58fd043337942ddbb9e Mon Sep 17 00:00:00 2001 From: Andrew n marshall Date: Tue, 13 Nov 2018 13:58:55 -0800 Subject: [PATCH 2/3] Temp save of work --- .../xcschemes/xcschememanagement.plist | 5 ++++ .../ios/Blockly WebView/ViewController.swift | 28 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/demos/mobile/ios/Blockly WebView.xcodeproj/xcuserdata/marshalla.xcuserdatad/xcschemes/xcschememanagement.plist b/demos/mobile/ios/Blockly WebView.xcodeproj/xcuserdata/marshalla.xcuserdatad/xcschemes/xcschememanagement.plist index 7cbeeade6..e1e71ff06 100644 --- a/demos/mobile/ios/Blockly WebView.xcodeproj/xcuserdata/marshalla.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/demos/mobile/ios/Blockly WebView.xcodeproj/xcuserdata/marshalla.xcuserdatad/xcschemes/xcschememanagement.plist @@ -9,6 +9,11 @@ orderHint 0 + Blockly WebView.xcscheme_^#shared#^_ + + orderHint + 0 + diff --git a/demos/mobile/ios/Blockly WebView/ViewController.swift b/demos/mobile/ios/Blockly WebView/ViewController.swift index dee718c07..a9952a857 100644 --- a/demos/mobile/ios/Blockly WebView/ViewController.swift +++ b/demos/mobile/ios/Blockly WebView/ViewController.swift @@ -69,6 +69,34 @@ class ViewController: UIViewController, WKUIDelegate { present(alert, animated: true) } + 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 + } + + let okTitle = NSLocalizedString("OK", comment: "OK button title") + let okAction = UIAlertAction(title: okTitle, style: .default) { (_) in + let textInput = alert.textFields![0] as UITextField + completionHandler(textInput.text) + } + alert.addAction(okAction) + + let cancelTitle = NSLocalizedString("Cancel", comment: "Cancel button title") + let cancelAction = UIAlertAction(title: cancelTitle, style: .cancel) { (_) in + completionHandler(nil) + } + alert.addAction(cancelAction) + + present(alert, animated: true) + } + override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. From 182ba961de2ff53b2c588961a16ff7987d39e8f8 Mon Sep 17 00:00:00 2001 From: Andrew n marshall Date: Wed, 14 Nov 2018 13:34:33 -0800 Subject: [PATCH 3/3] Doc comments and style corrections. --- .../ios/Blockly WebView/ViewController.swift | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/demos/mobile/ios/Blockly WebView/ViewController.swift b/demos/mobile/ios/Blockly WebView/ViewController.swift index a9952a857..f8ad8018e 100644 --- a/demos/mobile/ios/Blockly WebView/ViewController.swift +++ b/demos/mobile/ios/Blockly WebView/ViewController.swift @@ -1,6 +1,5 @@ -// // ViewController.swift -// Blockly WebView +// Blockly WebView UI controller. // // Created by Andrew Marshall on 8/7/18. // Copyright © 2018 Google. All rights reserved. @@ -9,13 +8,17 @@ import UIKit import WebKit + +/// A basic ViewController for a WebView. +/// It handles the initial page load, and functions like window.prompt(). 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" @IBOutlet weak var webView: WKWebView! - + + /// Additional setup after loading the UI NIB. override func viewDidLoad() { super.viewDidLoad() webView.uiDelegate = self @@ -23,12 +26,17 @@ class ViewController: UIViewController, WKUIDelegate { loadWebContent() } + /// Load the root HTML page into the webview. func loadWebContent() { - if let htmlUrl = Bundle.main.url(forResource: "webview", withExtension: "html", subdirectory: "Blockly") { + if let htmlUrl = Bundle.main.url(forResource: "webview", withExtension: "html", + subdirectory: "Blockly") { webView.load(URLRequest.init(url: htmlUrl)) + } else { + NSLog("Failed to load HTML. Could not find resource.") } } + /// Handle window.alert() with a native dialog. func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, @@ -44,6 +52,7 @@ class ViewController: UIViewController, WKUIDelegate { completionHandler() } + /// Handle window.confirm() with a native dialog. func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, @@ -62,13 +71,15 @@ class ViewController: UIViewController, WKUIDelegate { alert.addAction(ok) let cancelTitle = NSLocalizedString("Cancel", comment: "Cancel button title") - let cancel = UIAlertAction(title: cancelTitle, style: .default) { (action: UIAlertAction) -> Void in + let cancel = UIAlertAction(title: cancelTitle, style: .default) { + (action: UIAlertAction) -> Void in closeAndHandle(false) } alert.addAction(cancel) present(alert, animated: true) } + /// Handle window.prompt() with a native dialog. func webView(_ webView: WKWebView, runJavaScriptTextInputPanelWithPrompt prompt: String, defaultText: String?, @@ -96,10 +107,5 @@ class ViewController: UIViewController, WKUIDelegate { present(alert, animated: true) } - - override func didReceiveMemoryWarning() { - super.didReceiveMemoryWarning() - // Dispose of any resources that can be recreated. - } }