@@ -34,11 +34,68 @@ post_install do |installer|
3434 end
3535 end
3636end
37-
3837```
3938
4039Then, run the following command:
4140
4241```
4342$ pod install
4443```
44+
45+ ## Usage
46+ ``` swift
47+ import WKJavaScriptController
48+
49+ // Create protocol.
50+ @objc protocol JavaScriptInterface {
51+ func onSubmit (dictonary : [String : AnyObject ])
52+ func onSubmit (email : String , firstName : String , lastName : String , address1 : String , address2 : String , zipCode : JSInt, phoneNumber : String )
53+ func onCancel ()
54+ }
55+
56+ // Implement protocol.
57+ extension ViewController : JavaScriptInterface {
58+ func onSubmit (dictonary : [String : AnyObject ]) {
59+ NSLog (" onSubmit \( dictonary ) " )
60+ }
61+
62+ func onSubmit (email : String , firstName : String , lastName : String , address1 : String , address2 : String , zipCode : JSInt, phoneNumber : String ) {
63+ NSLog (" onSubmit \( email ) , \( firstName ) , \( lastName ) , \( address1 ) , \( address2 ) , \( zipCode.value ) , \( phoneNumber ) " )
64+ }
65+
66+ func onCancel () {
67+ NSLog (" onCancel" )
68+ }
69+ }
70+
71+ class ViewController : UIViewController {
72+ override func viewDidAppear (animated : Bool ) {
73+ super .viewDidAppear (animated)
74+
75+ // Create javaScriptController.
76+ let javaScriptController = WKJavaScriptController (name : " native" , target : self , bridgeProtocol : JavaScriptInterface.self )
77+
78+ // Add your javascript.
79+ let jsString = ...
80+ let userScript = WKUserScript (source : jsString, injectionTime : .AtDocumentEnd , forMainFrameOnly : true )
81+ javaScriptController.addUserScript (userScript)
82+
83+ let webView = WKWebView (... )
84+ ...
85+
86+ // Call prepareForJavaScriptController before initializing WKWebView or loading page.
87+ webView.prepareForJavaScriptController ()
88+ webView.loadRequest (... )
89+ }
90+
91+ ...
92+ }
93+ ```
94+ ``` js
95+ // In javascript.
96+ native .onSubmit ({
97+ ' first_name' : ' Davin' ,
98+ ' last_name' : ' Ahn' ,
99+ ' mail' : ' davin.ahn@ridi.com' ,
100+ });
101+ ```
0 commit comments