From 06a06f14185aea682e477d8a3ada7e152e57f8ef Mon Sep 17 00:00:00 2001 From: duwen Date: Tue, 18 Sep 2018 13:45:56 +0800 Subject: [PATCH] add JSCallback block typedef --- .../Debug-iphonesimulator/libdsbridge.a | Bin 346424 -> 346424 bytes README.md | 22 +++++++++++++----- dsBridge.podspec | 2 +- .../xcdebugger/Breakpoints_v2.xcbkptlist | 16 +++++++++++++ dsbridge/DWKWebView.h | 2 ++ dsbridge/dsbridge.h | 1 + dsbridgedemo/JsApiTest.h | 1 + dsbridgedemo/JsApiTest.m | 8 +++---- dsbridgedemo/JsApiTestSwift.swift | 3 ++- dsbridgedemo/JsEchoApi.m | 3 ++- readme-chs.md | 20 ++++++++++++---- 11 files changed, 60 insertions(+), 18 deletions(-) diff --git a/Build/Products/Debug-iphonesimulator/libdsbridge.a b/Build/Products/Debug-iphonesimulator/libdsbridge.a index 75a39163002d0b9148c3a7b44c319245f27108d9..a4d4e3a569cb24f5e277aff9414c16ab7c6d7c41 100644 GIT binary patch delta 384 zcmdlnNp!~~(Fxw{=0+wKMn)DJgEq24*~Xi>*ce^m93k&L_e6OGINLZG7?_k9xLiya z7?gP#80`6{Kh|XY!^pq=ffi%24jVrsNOZcRFQfGIl5j>*M*iuIfs8WSmxVJ{1Zl0T zVNzvewdCT^V`X5{72;wpJaGCjyF3{1sj4ZZ5Y9k|y%=UIhme-*wD{DBO0G<8`=yC(F u%Wc@ayh>19Ew_DIILqF0xP4LQSv+k)uI6WfxVix7>Kcfvckr+_*Z=@^1y>>f delta 346 zcmdlnNp!~~(Fxw{W=0mK<`yO!gEq2G-@KefZnGlWD;Ee$(|hSXQEmawHckcxCS?XL z7ZU~sWnKmbd*11fH5vaf@@{{i#aOJv#>)s2o$lz%C_TL-oKcjKcY0$Wqs;bY;fxhQ z?8|GIR2f+^YRBE`zU;kDgk6{B$DbZ0gu+3gkDOh2uGR&o4h23y5h zr^LXJ!Op;7%)7lUor%>3=FIJr7BfK|x;=3v(WFW-FT+Cs^=H*pl qW5bGYs>1eZ;VgU0Ax + // //npm - //npm install dsbridge@3.1.1 + //npm install dsbridge@3.1.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); } diff --git a/dsBridge.podspec b/dsBridge.podspec index cb01294..b309fc4 100644 --- a/dsBridge.podspec +++ b/dsBridge.podspec @@ -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. diff --git a/dsbridge.xcodeproj/xcuserdata/du.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/dsbridge.xcodeproj/xcuserdata/du.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist index bf198cb..09cb715 100644 --- a/dsbridge.xcodeproj/xcuserdata/du.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/dsbridge.xcodeproj/xcuserdata/du.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -339,5 +339,21 @@ landmarkType = "7"> + + + + diff --git a/dsbridge/DWKWebView.h b/dsbridge/DWKWebView.h index 929f5a6..365f526 100644 --- a/dsbridge/DWKWebView.h +++ b/dsbridge/DWKWebView.h @@ -8,6 +8,8 @@ #import +typedef void (^JSCallback)(NSString * _Nullable result,BOOL complete); + @interface DWKWebView : WKWebView @property (nullable, nonatomic, weak) id DSUIDelegate; diff --git a/dsbridge/dsbridge.h b/dsbridge/dsbridge.h index 2483075..a710eab 100644 --- a/dsbridge/dsbridge.h +++ b/dsbridge/dsbridge.h @@ -9,3 +9,4 @@ #import #import "JSBUtil.h" #import "DWKWebView.h" + diff --git a/dsbridgedemo/JsApiTest.h b/dsbridgedemo/JsApiTest.h index 39440fc..498829a 100644 --- a/dsbridgedemo/JsApiTest.h +++ b/dsbridgedemo/JsApiTest.h @@ -7,6 +7,7 @@ // #import +#import "dsbridge.h" @interface JsApiTest : NSObject diff --git a/dsbridgedemo/JsApiTest.m b/dsbridgedemo/JsApiTest.m index a1211cb..d1da4e6 100644 --- a/dsbridgedemo/JsApiTest.m +++ b/dsbridgedemo/JsApiTest.m @@ -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); } @@ -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; @@ -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{ } diff --git a/dsbridgedemo/JsApiTestSwift.swift b/dsbridgedemo/JsApiTestSwift.swift index f3c301e..4d84f73 100644 --- a/dsbridgedemo/JsApiTestSwift.swift +++ b/dsbridgedemo/JsApiTestSwift.swift @@ -7,6 +7,7 @@ // import Foundation +typealias JSCallback = (String, Bool)->Void class JsApiTestSwift: NSObject { @@ -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) } diff --git a/dsbridgedemo/JsEchoApi.m b/dsbridgedemo/JsEchoApi.m index aa61bf0..005e52b 100644 --- a/dsbridgedemo/JsEchoApi.m +++ b/dsbridgedemo/JsEchoApi.m @@ -7,6 +7,7 @@ // #import "JsEchoApi.h" +#import "dsbridge.h" @implementation JsEchoApi @@ -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); } diff --git a/readme-chs.md b/readme-chs.md index 0ad2655..4a0ca84 100644 --- a/readme-chs.md +++ b/readme-chs.md @@ -44,6 +44,8 @@ pod "dsBridge" 1. 新建一个类,实现API ```objective-c + #import "dsbridge.h" + ... @implementation JsApiTest //同步API - (NSString *) testSyn:(NSString *) msg @@ -51,7 +53,7 @@ pod "dsBridge" 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方式引入初始化代码(中国地区慢,建议下载到本地工程) - // + // //npm方式安装初始化代码 - //npm install dsbridge@3.1.1 + //npm install dsbridge@3.1.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); }