Skip to content

Commit dc578c7

Browse files
committed
Update async spec example
1 parent 7685aed commit dc578c7

3 files changed

Lines changed: 23 additions & 5 deletions

File tree

Demo/WKJavaScriptController-Demo/ViewController.swift

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,28 @@ import WKJavaScriptController
1717
extension ViewController: JavaScriptInterface {
1818
func onSubmit(_ dictonary: [String: AnyObject]) {
1919
NSLog("onSubmit \(dictonary)")
20-
isSubmitted = true
20+
_isSubmitted = true
2121
}
2222

2323
func onSubmit(_ dictonary: [String: AnyObject], clear: JSBool) {
2424
NSLog("onSubmit \(dictonary)")
2525
if clear.value {
2626
webView.evaluateJavaScript("clearAll()", completionHandler: nil)
2727
}
28-
isSubmitted = true
28+
_isSubmitted = true
2929
}
3030

3131
func onSubmit(_ email: String, firstName: String, lastName: String, address1: String, address2: String, zipCode: JSInt, phoneNumber: String) {
3232
NSLog("onSubmit \(email), \(firstName), \(lastName), \(address1), \(address2), \(zipCode.value), \(phoneNumber)")
33-
isSubmitted = true
33+
_isSubmitted = true
3434
}
3535

3636
func onCancel() {
3737
NSLog("onCancel")
38+
_isSubmitted = false
3839
}
3940

40-
var isSubmitted: JSBool { JSBool(isSubmitted) }
41+
var isSubmitted: JSBool { JSBool(_isSubmitted) }
4142

4243
func getErrorMessages(codes: [JSInt]) -> [String] {
4344
codes.map { "message\($0)" }
@@ -47,7 +48,7 @@ extension ViewController: JavaScriptInterface {
4748
class ViewController: UIViewController {
4849
fileprivate var webView: WKWebView!
4950

50-
private var isSubmitted = false
51+
private var _isSubmitted = false
5152

5253
override func viewDidAppear(_ animated: Bool) {
5354
super.viewDidAppear(animated)
@@ -63,6 +64,7 @@ class ViewController: UIViewController {
6364
javaScriptController.addUserScript(userScript)
6465

6566
webView = WKWebView(frame: view.frame)
67+
webView.uiDelegate = self
6668
view.addSubview(webView)
6769

6870
// Assign javaScriptController.
@@ -75,3 +77,13 @@ class ViewController: UIViewController {
7577
}
7678
}
7779
}
80+
81+
extension ViewController: WKUIDelegate {
82+
func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {
83+
let alertController = UIAlertController(title: nil, message: message, preferredStyle: .alert)
84+
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { _ in
85+
completionHandler()
86+
}))
87+
present(alertController, animated: true, completion: nil)
88+
}
89+
}

Demo/WKJavaScriptController-Demo/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@
2020
<br/>
2121
<input type="button" value="Submit" onclick="submit()">
2222
<input type="button" value="Cancel" onclick="native.onCancel()">
23+
<input type="button" value="Check" onclick="checkSumbit()">
2324
</body>
2425
</html>

Demo/WKJavaScriptController-Demo/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,8 @@ function clearAll() {
2727
function isChecked(id) {
2828
return document.getElementById(id).checked;
2929
};
30+
31+
async function checkSumbit() {
32+
const result = await native.isSubmitted;
33+
alert(result ? 'Submitted.' : 'Not submitted.');
34+
}

0 commit comments

Comments
 (0)