@@ -10,34 +10,62 @@ import UIKit
1010import WebKit
1111import 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+
2044class 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- }
0 commit comments