Skip to content

Commit 61f2265

Browse files
committed
- Add `logEnabled` property. (default: `true`) - Add `callbackTimeout` property. (default: `10`) - Rename `shouldSafeMethodCall` to `ignoreMethodCallWhenReceivedNull`. - Rename `shouldConvertJSONString` to `convertsToDictionaryWhenReceivedJsonString`. - Fix an issue where string composed of numbers were cast to `JSInt` by parsing top-level objects as `JSONSerialization` with `allowFragments` read option.
1 parent 61cf598 commit 61f2265

File tree

6 files changed

+358
-98
lines changed

6 files changed

+358
-98
lines changed

CHANGELOG.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,20 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77

88
## [Unreleased]
99

10-
* None.
10+
### Added
11+
12+
* Support native return to JavaScript as [Promise](https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Promise).
13+
* Add `logEnabled` property. (default: `true`)
14+
* Add `callbackTimeout` property. (default: `10`)
15+
16+
### Changed
17+
18+
* Rename `shouldSafeMethodCall` to `ignoreMethodCallWhenReceivedNull`.
19+
* Rename `shouldConvertJSONString` to `convertsToDictionaryWhenReceivedJsonString`.
20+
21+
### Fixed
22+
23+
* Fix an issue where string composed of numbers were cast to `JSInt` by parsing top-level objects as `JSONSerialization` with `allowFragments` read option.
1124

1225
## [1.2.0 (2019-02-26)]
1326

@@ -70,7 +83,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
7083
### Added
7184

7285
* Add method invocation notification.
73-
* Add `shouldSafeMethodCall` option.
86+
* Add `shouldSafeMethodCall` property.
7487

7588
## [1.1.0 (2017-01-18)]
7689

README.md

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
# WKJavaScriptController
2+
23
Calling native code from Javascript in iOS likes JavascriptInterface in Android.
34

45
[![CocoaPods Compatible](https://img.shields.io/cocoapods/v/WKJavaScriptController.svg?style=flat)](https://cocoadocs.org/docsets/WKJavaScriptController)
56
[![Platform](https://img.shields.io/cocoapods/p/WKJavaScriptController.svg?style=flat)](https://cocoadocs.org/docsets/WKJavaScriptController)
67
[![License](https://img.shields.io/cocoapods/l/WKJavaScriptController.svg?style=flat)](https://cocoadocs.org/docsets/WKJavaScriptController)
78

89
## Requirements
10+
911
- Xcode 10.0+
1012
- Swift 4.2
1113
- iOS8+
1214

1315
(based on WKJavaScriptController 1.2.0+)
1416

1517
## Installation
18+
1619
This library is distributed by [CocoaPods](https://cocoapods.org).
1720

1821
CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:
@@ -40,6 +43,7 @@ $ pod install
4043
```
4144

4245
## Usage
46+
4347
```swift
4448
import WKJavaScriptController
4549

@@ -49,6 +53,8 @@ import WKJavaScriptController
4953
func onSubmit(_ dictonary: [String: AnyObject])
5054
func onSubmit(_ email: String, firstName: String, lastName: String, address1: String, address2: String, zipCode: JSInt, phoneNumber: String)
5155
func onCancel()
56+
var isSubmitted: JSBool { get }
57+
@objc optional func getErrorMessages(codes: [JSInt]) -> [String]
5258
}
5359

5460
// Implement protocol.
@@ -64,6 +70,14 @@ extension ViewController: JavaScriptInterface {
6470
func onCancel() {
6571
NSLog("onCancel")
6672
}
73+
74+
var isSubmitted: JSBool {
75+
return JSBool(true)
76+
}
77+
78+
func getErrorMessages(codes: [JSInt]) -> [String] {
79+
return ["dummy1", "dummy2", "dummy3"]
80+
}
6781
}
6882

6983
class ViewController: UIViewController {
@@ -101,10 +115,24 @@ native.onSubmit({
101115
});
102116
```
103117

118+
Can receive native return in JavaScript as [Promise](https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Promise):
119+
120+
```js
121+
// In javascript.
122+
const isSubmitted = await native.isSubmitted;
123+
// or native.isSubmitted.then(isSubmitted => ...);
124+
const messages = await native.getErrorMessages([200, 400, 500]);
125+
// or native.getErrorMessages([200, 400, 500]).then(messages => ...);
126+
```
127+
104128
## Limitations
105-
- Can not receive native return in JavaScript.
129+
130+
- Can not receive native return in JavaScript as sync. can only async return.
106131
- Method argument length is up to 10.
107-
- Allowed argument types are NSNumber, NSString, NSDate, NSArray, NSDictionary, and NSNull(when `undefined` passed).
108-
- If Value types of Swift(Bool, Int32, Int, Float, Double, ...) used in argument that are not wrapped in NSArray or NSDictionary, it must be replaced with JSBool, JSInt or JSFloat.
109-
(Because Value types of Swift in ObjC is replaced by NSNumber.)
132+
- Allowed argument types are String, Date, Array, Dictionary, JSBool, JSInt, JSFloat, NSNumber and NSNull(when `undefined` or `null` passed from JavaScript).
133+
- If Swift value types(Bool, Int32, Int, Float, Double, ...) used in argument, it must be replaced with JSBool, JSInt or JSFloat. (Because Swift value type is replaced by NSNumber in ObjC.)
110134
- Class methods in protocol are not supported.
135+
136+
## License
137+
138+
[MIT](https://github.com/ridi/WKJavaScriptController/blob/master/LICENSE)

WKJavaScriptController-Demo/WKJavaScriptController-Demo.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
/* Begin PBXFileReference section */
2121
272FAEE1CFFC059EDAA9D22F /* Pods_WKJavaScriptController_Demo.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_WKJavaScriptController_Demo.framework; sourceTree = BUILT_PRODUCTS_DIR; };
22+
2A34D7D622277CB800250021 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../../README.md; sourceTree = "<group>"; };
23+
2A34D7D722277CC200250021 /* CHANGELOG.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; name = CHANGELOG.md; path = ../../CHANGELOG.md; sourceTree = "<group>"; };
2224
2CC90156977464A015EF3DCC /* Pods-WKJavaScriptController-Demo.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-WKJavaScriptController-Demo.debug.xcconfig"; path = "Target Support Files/Pods-WKJavaScriptController-Demo/Pods-WKJavaScriptController-Demo.debug.xcconfig"; sourceTree = "<group>"; };
2325
83D5383F1E2C960D00E42B6A /* index.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = index.html; sourceTree = "<group>"; };
2426
83D538411E2C978000E42B6A /* index.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = index.js; sourceTree = "<group>"; };
@@ -65,6 +67,8 @@
6567
83FA358C1E2C5C6900A6B171 /* WKJavaScriptController-Demo */ = {
6668
isa = PBXGroup;
6769
children = (
70+
2A34D7D622277CB800250021 /* README.md */,
71+
2A34D7D722277CC200250021 /* CHANGELOG.md */,
6872
83FA358D1E2C5C6900A6B171 /* AppDelegate.swift */,
6973
83FA358F1E2C5C6900A6B171 /* ViewController.swift */,
7074
83FA35911E2C5C6900A6B171 /* Main.storyboard */,

WKJavaScriptController-Demo/WKJavaScriptController-Demo/ViewController.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import WKJavaScriptController
99
func onSubmit(_ dictonary: [String: AnyObject], clear: JSBool)
1010
func onSubmit(_ email: String, firstName: String, lastName: String, address1: String, address2: String, zipCode: JSInt, phoneNumber: String)
1111
func onCancel()
12+
var isSubmitted: JSBool { get }
13+
@objc optional func getErrorMessages(codes: [JSInt]) -> [String]
1214
}
1315

1416
// Implement protocol.
@@ -31,6 +33,14 @@ extension ViewController: JavaScriptInterface {
3133
func onCancel() {
3234
NSLog("onCancel")
3335
}
36+
37+
var isSubmitted: JSBool {
38+
return JSBool(true)
39+
}
40+
41+
func getErrorMessages(codes: [JSInt]) -> [String] {
42+
return codes.map { "message\($0)" }
43+
}
3444
}
3545

3646
class ViewController: UIViewController {

WKJavaScriptController-Demo/WKJavaScriptController-Demo/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function submit() {
1212
if (isChecked('input_json')) {
1313
native.onSubmit(values);
1414
} else if (isChecked('input_literal')) {
15-
native.onSubmitWithFirstnameAndLastnameAndAddress1AndAddress2AndZipcodeAndPhonenumber(values['mail'], values['first_name'], values['last_name'], values['address_line_1'], values['address_line_2'], parseInt(values['zip_code']), values['phone_number']);
15+
native.onSubmitWithFirstnameAndLastnameAndAddress1AndAddress2AndZipcodeAndPhonenumber(values['mail'], values['first_name'], values['last_name'], values['address_line_1'], values['address_line_2'], parseInt(values['zip_code']), `${values['phone_number']}`);
1616
}
1717
}
1818

0 commit comments

Comments
 (0)