Skip to content

Commit

Permalink
add JSCallback block typedef
Browse files Browse the repository at this point in the history
  • Loading branch information
duwen committed Sep 18, 2018
1 parent cf556ee commit 06a06f1
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 18 deletions.
Binary file modified Build/Products/Debug-iphonesimulator/libdsbridge.a
Binary file not shown.
22 changes: 16 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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")
```

Expand Down Expand Up @@ -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.

Expand All @@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion dsBridge.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |s|
#

s.name = "dsBridge"
s.version = "3.0.3"
s.version = "3.0.4"
s.summary = "An ios bridge for calling functions synchronously and asynchronously between JavaScript and Object-C in WKWebView/UIWebView"

# This description is used to generate tags and improve search results.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,5 +339,21 @@
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "dsbridgedemo/JsApiTest.m"
timestampString = "558942264.07623"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "26"
endingLineNumber = "26"
landmarkName = "-testAsyn::"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
2 changes: 2 additions & 0 deletions dsbridge/DWKWebView.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#import <WebKit/WebKit.h>

typedef void (^JSCallback)(NSString * _Nullable result,BOOL complete);

@interface DWKWebView : WKWebView <WKUIDelegate>

@property (nullable, nonatomic, weak) id <WKUIDelegate> DSUIDelegate;
Expand Down
1 change: 1 addition & 0 deletions dsbridge/dsbridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
#import <Foundation/Foundation.h>
#import "JSBUtil.h"
#import "DWKWebView.h"

1 change: 1 addition & 0 deletions dsbridgedemo/JsApiTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import <Foundation/Foundation.h>
#import "dsbridge.h"

@interface JsApiTest : NSObject

Expand Down
8 changes: 4 additions & 4 deletions dsbridgedemo/JsApiTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ - (NSString *) testSyn: (NSString *) msg
return [msg stringByAppendingString:@"[ syn call]"];
}

- (void) testAsyn:(NSString *) msg :(void (^)(NSString * _Nullable result,BOOL complete))completionHandler
- (void) testAsyn:(NSString *) msg :(JSCallback) completionHandler
{
completionHandler([msg stringByAppendingString:@" [ asyn call]"],YES);
}
Expand All @@ -31,12 +31,12 @@ - (NSString *)testNoArgSyn:(NSDictionary *) args
return @"testNoArgSyn called [ syn call]";
}

- ( void )testNoArgAsyn:(NSDictionary *) args :(void (^)(NSString * _Nullable result,BOOL complete))completionHandler
- ( void )testNoArgAsyn:(NSDictionary *) args :(JSCallback)completionHandler
{
completionHandler(@"testNoArgAsyn called [ asyn call]",YES);
}

- ( void )callProgress:(NSDictionary *) args :(void (^)(NSNumber * _Nullable result,BOOL complete))completionHandler
- ( void )callProgress:(NSDictionary *) args :(JSCallback)completionHandler
{
value=10;
hanlder=completionHandler;
Expand All @@ -62,7 +62,7 @@ -(void)onTimer:t{
* redirect requests to native, more about Fly see https://github.com/wendux/fly
* @param requestInfo passed by fly.js, more detail reference https://wendux.github.io/dist/#/doc/flyio-en/native
*/
-(void)onAjaxRequest:(NSDictionary *) requestInfo :(void (^)(NSString * _Nullable result,BOOL complete))completionHandle{
-(void)onAjaxRequest:(NSDictionary *) requestInfo :(JSCallback)completionHandler{

}

Expand Down
3 changes: 2 additions & 1 deletion dsbridgedemo/JsApiTestSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
typealias JSCallback = (String, Bool)->Void

class JsApiTestSwift: NSObject {

Expand All @@ -15,7 +16,7 @@ class JsApiTestSwift: NSObject {
return String(format:"%@[Swift sync call:%@]", arg, "test")
}

@objc func testAsyn( _ arg:String, handler: (String, Bool)->Void) {
@objc func testAsyn( _ arg:String, handler: JSCallback) {
handler(String(format:"%@[Swift async call:%@]", arg, "test"), true)
}

Expand Down
3 changes: 2 additions & 1 deletion dsbridgedemo/JsEchoApi.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import "JsEchoApi.h"
#import "dsbridge.h"

@implementation JsEchoApi

Expand All @@ -15,7 +16,7 @@ - (id) syn:(id) arg
return arg;
}

- (void) asyn: (id) arg :(void (^)( id _Nullable result,BOOL complete))completionHandler
- (void) asyn: (id) arg :(JSCallback)completionHandler
{
completionHandler(arg,YES);
}
Expand Down
20 changes: 15 additions & 5 deletions readme-chs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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")
```

Expand Down Expand Up @@ -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)。
>

Expand All @@ -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分类管理,并且命名空间支持多级的,不同级之间只需用'.' 分隔即可。
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 06a06f1

Please sign in to comment.