-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
duwen
committed
Sep 18, 2018
1 parent
cf556ee
commit 06a06f1
Showing
11 changed files
with
60 additions
and
18 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,14 +67,16 @@ To use a dsBridge in your own project: | |
1. Implement APIs in a class | ||
|
||
```objective-c | ||
#import "dsbridge.h" | ||
... | ||
@implementation JsApiTest | ||
//for synchronous invocation | ||
- (NSString *) testSyn:(NSString *) msg | ||
{ | ||
return [msg stringByAppendingString:@"[ syn call]"]; | ||
} | ||
//for asynchronous invocation | ||
- (void) testAsyn:(NSString *) msg :(void (^)(NSString * _Nullable result,BOOL complete))completionHandler | ||
- (void) testAsyn:(NSString *) msg :(JSCallback)completionHandler | ||
{ | ||
completionHandler([msg stringByAppendingString:@" [ asyn call]"],YES); | ||
} | ||
|
@@ -95,9 +97,9 @@ To use a dsBridge in your own project: | |
|
||
```javascript | ||
//cdn | ||
//<script src="https://unpkg.com/[email protected].1/dist/dsbridge.js"> </script> | ||
//<script src="https://cdn.jsdelivr.net/npm/[email protected].4/dist/dsbridge.js"> //</script> | ||
//npm | ||
//npm install [email protected].1 | ||
//npm install [email protected].4 | ||
var dsBridge=require("dsbridge") | ||
``` | ||
|
||
|
@@ -142,7 +144,13 @@ In order to be compatible with IOS , we make the following convention on Object | |
2. For asynchronous API. | ||
**` (void) handler:(id) arg :(void (^)( id result,BOOL complete))completionHandler)`** | ||
**` (void) handler:(id) arg :(JSCallback)completionHandler)`** | ||
`JSCallback` is a block type: | ||
```objective-c | ||
typedef void (^JSCallback)(NSString * _Nullable result,BOOL complete); | ||
``` | ||
> Attention: API name can't start with "init", because it is reserved in OC class. | ||
|
||
|
@@ -166,6 +174,8 @@ Two points you should keep in mind: | |
- Must add "@objc" to Swift API. | ||
- Must use "_" to ignore the first argument name explicitly | ||
|
||
The complete example is [here](https://github.com/wendux/DSBridge-IOS/blob/master/dsbridgedemo/JsApiTestSwift.swift) . | ||
|
||
## Namespace | ||
|
||
Namespaces can help you better manage your APIs, which is very useful in hybrid applications, because these applications have a large number of APIs. DSBridge (>= v3.0.0) allows you to classify API with namespace. And the namespace can be multilevel, between different levels with '.' division. | ||
|
@@ -188,7 +198,7 @@ Normally, when a API is called to end, it returns a result, which corresponds on | |
In Object-c | ||
|
||
```objective-c | ||
- ( void )callProgress:(NSDictionary *) args :(void (^)(NSNumber * _Nullable result,BOOL complete))completionHandler | ||
- ( void )callProgress:(NSDictionary *) args :(JSCallback)completionHandler | ||
{ | ||
value=10; | ||
hanlder=completionHandler; | ||
|
@@ -256,7 +266,7 @@ Example: | |
{ | ||
return arg; | ||
} | ||
- (void) asyn: (id) arg :(void (^)( id _Nullable result,BOOL complete))completionHandler | ||
- (void) asyn: (id) arg :(JSCallback)completionHandler | ||
{ | ||
completionHandler(arg,YES); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ | |
#import <Foundation/Foundation.h> | ||
#import "JSBUtil.h" | ||
#import "DWKWebView.h" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
// | ||
|
||
#import <Foundation/Foundation.h> | ||
#import "dsbridge.h" | ||
|
||
@interface JsApiTest : NSObject | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,14 +44,16 @@ pod "dsBridge" | |
1. 新建一个类,实现API | ||
|
||
```objective-c | ||
#import "dsbridge.h" | ||
... | ||
@implementation JsApiTest | ||
//同步API | ||
- (NSString *) testSyn:(NSString *) msg | ||
{ | ||
return [msg stringByAppendingString:@"[ syn call]"]; | ||
} | ||
//异步API | ||
- (void) testAsyn:(NSString *) msg :(void (^)(NSString * _Nullable result,BOOL complete))completionHandler | ||
- (void) testAsyn:(NSString *) msg :(JSCallback)completionHandler | ||
{ | ||
completionHandler([msg stringByAppendingString:@" [ asyn call]"],YES); | ||
} | ||
|
@@ -73,9 +75,9 @@ pod "dsBridge" | |
|
||
```javascript | ||
//cdn方式引入初始化代码(中国地区慢,建议下载到本地工程) | ||
//<script src="https://unpkg.com/[email protected].1/dist/dsbridge.js"> </script> | ||
//<script src="https://cdn.jsdelivr.net/npm/[email protected].4/dist/dsbridge.js"> //</script> | ||
//npm方式安装初始化代码 | ||
//npm install [email protected].1 | ||
//npm install [email protected].4 | ||
var dsBridge=require("dsbridge") | ||
``` | ||
|
||
|
@@ -120,6 +122,12 @@ pod "dsBridge" | |
|
||
**` (void) handler:(id)arg :(void (^)( id result,BOOL complete))completionHandler)`** | ||
|
||
`JSCallback` 是一个block类型: | ||
|
||
```objective-c | ||
typedef void (^JSCallback)(NSString * _Nullable result,BOOL complete); | ||
``` | ||
|
||
> 注意:API名字**不能**以"init"开始,因为oc的类中是被预留的, 如果以"init"开始,执行结果将无法预期(很多时候会crash)。 | ||
> | ||
|
||
|
@@ -145,6 +153,8 @@ pod "dsBridge" | |
- 必须给Swift API添加 "@objc" 标注。 | ||
- 必须给第一个参数前添加下划线"_"来显式忽略参数名 | ||
|
||
完整的示例在 [这里](https://github.com/wendux/DSBridge-IOS/blob/master/dsbridgedemo/JsApiTestSwift.swift) . | ||
|
||
## 命名空间 | ||
|
||
命名空间可以帮助你更好的管理API,这在API数量多的时候非常实用,比如在混合应用中。DSBridge (>= v3.0.0) 支持你通过命名空间将API分类管理,并且命名空间支持多级的,不同级之间只需用'.' 分隔即可。 | ||
|
@@ -169,7 +179,7 @@ pod "dsBridge" | |
In Object-c | ||
|
||
```objective-c | ||
- ( void )callProgress:(NSDictionary *) args :(void (^)(NSNumber * _Nullable result,BOOL complete))completionHandler | ||
- ( void )callProgress:(NSDictionary *) args :(JSCallback)completionHandler | ||
{ | ||
value=10; | ||
hanlder=completionHandler; | ||
|
@@ -237,7 +247,7 @@ DSBridge已经实现了 Javascript的弹出框函数(alert/confirm/prompt),这 | |
{ | ||
return arg; | ||
} | ||
- (void) asyn: (id) arg :(void (^)( id _Nullable result,BOOL complete))completionHandler | ||
- (void) asyn: (id) arg :(JSCallback)completionHandler | ||
{ | ||
completionHandler(arg,YES); | ||
} | ||
|