Skip to content

Commit 2e0b987

Browse files
committed
Update demo
1 parent 941b42d commit 2e0b987

File tree

2 files changed

+41
-36
lines changed

2 files changed

+41
-36
lines changed

WKJavaScriptController-Demo/WKJavaScriptController-Demo/ViewController.swift

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,62 @@ import UIKit
1010
import WebKit
1111
import WKJavaScriptController
1212

13+
// Create protocol.
14+
// '@objc' keyword is required. because method call is based on ObjC.
1315
@objc protocol JavaScriptInterface {
1416
func onSubmit(_ dictonary: [String: AnyObject])
1517
func onSubmit(_ dictonary: [String: AnyObject], clear: JSBool)
1618
func onSubmit(_ email: String, firstName: String, lastName: String, address1: String, address2: String, zipCode: JSInt, phoneNumber: String)
1719
func onCancel()
1820
}
1921

22+
// Implement protocol.
23+
extension ViewController: JavaScriptInterface {
24+
func onSubmit(_ dictonary: [String: AnyObject]) {
25+
NSLog("onSubmit \(dictonary)")
26+
}
27+
28+
func onSubmit(_ dictonary: [String: AnyObject], clear: JSBool) {
29+
NSLog("onSubmit \(dictonary)")
30+
if clear.value {
31+
webView.evaluateJavaScript("clearAll()", completionHandler: nil)
32+
}
33+
}
34+
35+
func onSubmit(_ email: String, firstName: String, lastName: String, address1: String, address2: String, zipCode: JSInt, phoneNumber: String) {
36+
NSLog("onSubmit \(email), \(firstName), \(lastName), \(address1), \(address2), \(zipCode.value), \(phoneNumber)")
37+
}
38+
39+
func onCancel() {
40+
NSLog("onCancel")
41+
}
42+
}
43+
2044
class ViewController: UIViewController {
2145
fileprivate var webView: WKWebView!
2246

2347
override func viewDidAppear(_ animated: Bool) {
2448
super.viewDidAppear(animated)
2549

2650
if webView == nil {
51+
// Create javaScriptController.
2752
let javaScriptController = WKJavaScriptController(name: "native", target: self, bridgeProtocol: JavaScriptInterface.self)
2853

54+
// [Optional] Add your javascript.
2955
let jsPath = Bundle.main.path(forResource: "index", ofType: "js")!
3056
let jsString = try! String(contentsOfFile: jsPath, encoding: String.Encoding.utf8)
3157
let userScript = WKUserScript(source: jsString, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
3258
javaScriptController.addUserScript(userScript)
3359

3460
webView = WKWebView(frame: view.frame)
35-
webView.javaScriptController = javaScriptController
3661
view.addSubview(webView)
3762

63+
// Assign javaScriptController.
64+
webView.javaScriptController = javaScriptController
65+
3866
let htmlPath = Bundle.main.path(forResource: "index", ofType: "html")!
3967
let htmlString = try! String(contentsOfFile: htmlPath, encoding: String.Encoding.utf8)
40-
webView.prepareForJavaScriptController()
68+
webView.prepareForJavaScriptController() // Call prepareForJavaScriptController before initializing WKWebView or loading page.
4169
webView.loadHTMLString(htmlString, baseURL: Bundle.main.bundleURL)
4270
}
4371
}
@@ -46,26 +74,3 @@ class ViewController: UIViewController {
4674
return true
4775
}
4876
}
49-
50-
// MARK: - JavaScriptInterface
51-
52-
extension ViewController: JavaScriptInterface {
53-
func onSubmit(_ dictonary: [String: AnyObject]) {
54-
NSLog("onSubmit \(dictonary)")
55-
}
56-
57-
func onSubmit(_ dictonary: [String: AnyObject], clear: JSBool) {
58-
NSLog("onSubmit \(dictonary)")
59-
if clear.value {
60-
webView.evaluateJavaScript("clearAll()", completionHandler: nil)
61-
}
62-
}
63-
64-
func onSubmit(_ email: String, firstName: String, lastName: String, address1: String, address2: String, zipCode: JSInt, phoneNumber: String) {
65-
NSLog("onSubmit \(email), \(firstName), \(lastName), \(address1), \(address2), \(zipCode.value), \(phoneNumber)")
66-
}
67-
68-
func onCancel() {
69-
NSLog("onCancel")
70-
}
71-
}

WKJavaScriptController/WKJavaScriptController.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ open class WKJavaScriptController: NSObject {
179179
}
180180

181181
bridgeList.append(bridge)
182-
log("Read \(bridge.nativeSelector) -> \(bridge.jsSelector)")
182+
log("Parsed \(bridge.nativeSelector) -> \(bridge.jsSelector)")
183183
}
184184
free(methodList)
185185
}
@@ -270,17 +270,17 @@ extension WKJavaScriptController: WKScriptMessageHandler {
270270
func cast(_ arg: Arg) -> Arg {
271271
if let number = arg as? NSNumber,
272272
let type = String(cString: number.objCType, encoding: String.Encoding.utf8) {
273-
switch type {
274-
case "c", "C", "B":
275-
return JSBool(value: number)
276-
default:
277-
if number.stringValue.range(of: ".") != nil {
278-
return JSFloat(value: number)
279-
} else if number.stringValue == "nan" {
280-
return JSInt(value: NSNumber(value: 0 as Int))
273+
switch type {
274+
case "c", "C", "B":
275+
return JSBool(value: number)
276+
default:
277+
if number.stringValue.range(of: ".") != nil {
278+
return JSFloat(value: number)
279+
} else if number.stringValue == "nan" {
280+
return JSInt(value: NSNumber(value: 0 as Int))
281+
}
282+
return JSInt(value: number)
281283
}
282-
return JSInt(value: number)
283-
}
284284
}
285285
return arg
286286
}

0 commit comments

Comments
 (0)