22KWebView help you to call native code from Javascript in the iOS application.
33Written by Swift 2.0. It's similar WebView.addJavascriptInterface in the Android application.
44
5- # Setting your project
5+ ## Setting your project
66
77Step 1:
88Expand the Link Binary With Libraries section and add the following item:
99JavaScriptCore.framework
1010
1111Step 2:
12- - Define a protocol, inherit from JSExport protocol, which contains these native functions as you want to work with JavaScript
12+ - Define a protocol, inherit from JSExport protocol, which contains these native functions as you want to work with JavaScript.
1313For example:
14+
15+ ``` swift
1416@objc protocol MyExport : JSExport
1517{
1618 func check (message : String )
1719 func sayGreeting (message : String , _ name : String )
1820 func anotherSayGreeting (message : String , name : String )
1921 func showDialog (title : String , _ message : String )
2022}
23+ ```
2124
2225Step 3:
23- Define a class to implement native functions above
24- For example:
26+ Define a class to implement native functions above.
27+ ``` swift
2528class JSInterface : NSObject , MyExport
2629{
2730 func check (message : String ) {
@@ -45,27 +48,31 @@ class JSInterface : NSObject, MyExport
4548 })
4649 }
4750}
48-
51+ ```
4952
5053Step 4:
5154From Indentity Inspector of your UIWebView, at Custom class section, update Class attribute to KWebView
5255
5356Step 5:
5457Create an outlet to the webview in your view controller
58+ ``` swift
5559@IBOutlet weak var webView : KWebView!
60+ ```
5661
5762Step 6:
5863Add java script interface as below:
64+ ``` swift
5965self .webView .addJavascriptInterface (JSInterface (), forKey : " Native" );
60-
61- # Inuse
62-
66+ ```
67+ ##Usage
6368Now to call those native functions above from JavaScipt is loaded your webview, just call:
6469
70+ ``` swift
6571Native.check ()
72+
6673Native.sayGreeting ('Hello', 'JavaScript Interface')
67- ..
6874
75+ ```
6976
7077
7178
0 commit comments