Skip to content

Commit 0a16ebe

Browse files
committed
Update README.md
1 parent e67100f commit 0a16ebe

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

README.md

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,29 @@
22
KWebView help you to call native code from Javascript in the iOS application.
33
Written by Swift 2.0. It's similar WebView.addJavascriptInterface in the Android application.
44

5-
# Setting your project
5+
## Setting your project
66

77
Step 1:
88
Expand the Link Binary With Libraries section and add the following item:
99
JavaScriptCore.framework
1010

1111
Step 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.
1313
For 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

2225
Step 3:
23-
Define a class to implement native functions above
24-
For example:
26+
Define a class to implement native functions above.
27+
```swift
2528
class JSInterface : NSObject, MyExport
2629
{
2730
func check(message: String) {
@@ -45,27 +48,31 @@ class JSInterface : NSObject, MyExport
4548
})
4649
}
4750
}
48-
51+
```
4952

5053
Step 4:
5154
From Indentity Inspector of your UIWebView, at Custom class section, update Class attribute to KWebView
5255

5356
Step 5:
5457
Create an outlet to the webview in your view controller
58+
```swift
5559
@IBOutlet weak var webView : KWebView!
60+
```
5661

5762
Step 6:
5863
Add java script interface as below:
64+
```swift
5965
self.webView.addJavascriptInterface(JSInterface(), forKey: "Native");
60-
61-
# Inuse
62-
66+
```
67+
##Usage
6368
Now to call those native functions above from JavaScipt is loaded your webview, just call:
6469

70+
```swift
6571
Native.check()
72+
6673
Native.sayGreeting('Hello', 'JavaScript Interface')
67-
..
6874

75+
```
6976

7077

7178

0 commit comments

Comments
 (0)