Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

优化OC代码格式规范 #140

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 33 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
English| [中文简体](https://github.com/wendux/DSBridge-IOS/blob/master/readme-chs.md)
# DSBridge for IOS
# DSBridge for iOS

![dsBridge](https://github.com/wendux/DSBridge-IOS/raw/master/img/dsbridge.png)

Expand Down Expand Up @@ -70,12 +70,12 @@ To use a dsBridge in your own project:
...
@implementation JsApiTest
//for synchronous invocation
- (NSString *) testSyn:(NSString *) msg
- (NSString *)testSyn:(NSString *)msg
{
return [msg stringByAppendingString:@"[ syn call]"];
}
//for asynchronous invocation
- (void) testAsyn:(NSString *) msg :(JSCallback)completionHandler
- (void)testAsyn:(NSString *)msg callback:(JSCallback)completionHandler
{
completionHandler([msg stringByAppendingString:@" [ asyn call]"],YES);
}
Expand All @@ -85,7 +85,7 @@ To use a dsBridge in your own project:
2. add API object to DWKWebView

```objective-c
DWKWebView * dwebview=[[DWKWebView alloc] initWithFrame:bounds];
DWKWebView *dwebview = [[DWKWebView alloc] initWithFrame:bounds];
// register api object without namespace
[dwebview addJavascriptObject:[[JsApiTest alloc] init] namespace:nil];
```
Expand Down Expand Up @@ -197,21 +197,21 @@ Normally, when a API is called to end, it returns a result, which corresponds on
In Object-c

```objective-c
- ( void )callProgress:(NSDictionary *) args :(JSCallback)completionHandler
{
value=10;
hanlder=completionHandler;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(onTimer:)
userInfo:nil
repeats:YES];
- (void)callProgress:(NSDictionary *)args callback:(JSCallback)completionHandler {
value = 10;
hanlder = completionHandler;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(onTimer:)
userInfo:nil
repeats:YES];
}
-(void)onTimer:t{
if(value!=-1){
hanlder([NSNumber numberWithInt:value--],NO);
}else{
hanlder(@"",YES);

- (void)onTimer:t {
if (value != -1) {
hanlder([NSNumber numberWithInt:value--], NO);
} else {
hanlder(@"", YES);
[timer invalidate];
}
}
Expand Down Expand Up @@ -261,14 +261,14 @@ Example:

```objective-c
@implementation JsEchoApi
- (id) syn:(id) arg
{
- (id)syn:(id)arg {
return arg;
}
- (void) asyn: (id) arg :(JSCallback)completionHandler
{
completionHandler(arg,YES);

- (void)asyn:(id)arg callback:(JSCallback)completionHandler {
completionHandler(arg, YES);
}

@end
// register api object with namespace "echo"
[dwebview addJavascriptObject:[[JsEchoApi alloc] init] namespace:@"echo"];
Expand All @@ -282,7 +282,7 @@ var ret=dsBridge.call("echo.syn",{msg:" I am echoSyn call", tag:1})
alert(JSON.stringify(ret))
// call echo.asyn
dsBridge.call("echo.asyn",{msg:" I am echoAsyn call",tag:2},function (ret) {
alert(JSON.stringify(ret));
alert(JSON.stringify(ret));
})
```

Expand All @@ -294,11 +294,11 @@ Remove the Object-c API object with supplied namespace.



##### `callHandler:(NSString *) methodName arguments:(NSArray *) args`
##### `callHandler:(NSString *)methodName arguments:(NSArray *)args`

##### `callHandler:(NSString *) methodName completionHandler:(void (^)(id value))completionHandler`
##### `callHandler:(NSString *)methodName completionHandler:(void (^)(id value))completionHandler`

##### `callHandler:(NSString *) methodName arguments:(NSArray *) args completionHandler:(void (^ )(id value))completionHandler`
##### `callHandler:(NSString *)methodName arguments:(NSArray *)args completionHandler:(void (^ )(id value))completionHandler`

Call the javascript API. If a `completionHandler` is given, the javascript handler can respond. the `methodName` can contain the namespace. **The completionHandler will be called in main thread**.

Expand All @@ -307,11 +307,11 @@ Example:
```objective-c
[dwebview callHandler:@"append" arguments:@[@"I",@"love",@"you"]
completionHandler:^(NSString * _Nullable value) {
NSLog(@"call succeed, append string is: %@",value);
NSLog(@"call succeed, append string is: %@",value);
}];
// call with namespace 'syn', More details to see the Demo project
[dwebview callHandler:@"syn.getInfo" completionHandler:^(NSDictionary * _Nullable value) {
NSLog(@"Namespace syn.getInfo: %@",value);
NSLog(@"Namespace syn.getInfo: %@",value);
}];
```

Expand All @@ -324,7 +324,7 @@ BE CAREFUL to use. if you call any of the javascript popup box functions (`alert
Example:

```objective-c
[dwebview disableJavascriptDialogBlock: true]
[dwebview disableJavascriptDialogBlock:true]
```

if you want to enable the block, just calling this method with the argument value `false` .
Expand All @@ -339,7 +339,7 @@ Example:

```objective-c
[dwebview setJavascriptCloseWindowListener:^{
NSLog(@"window.close called");
NSLog(@"window.close called");
}];
```

Expand All @@ -354,7 +354,7 @@ Example:
```objective-c
// test if javascript method exists.
[dwebview hasJavascriptMethod:@"addValue" methodExistCallback:^(bool exist) {
NSLog(@"method 'addValue' exist : %d",exist);
NSLog(@"method 'addValue' exist : %d",exist);
}];
```

Expand Down Expand Up @@ -430,7 +430,7 @@ Register javascript synchronous and asynchronous API for Native invocation. The
```objective-c
// call javascript method
[dwebview callHandler:@"addValue" arguments:@[@3,@4] completionHandler:^(NSNumber * value){
NSLog(@"%@",value);
NSLog(@"%@",value);
}];

[dwebview callHandler:@"append" arguments:@[@"I",@"love",@"you"] completionHandler:^(NSString * _Nullable value) {
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.6"
s.version = "3.0.7"
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
5 changes: 3 additions & 2 deletions dsbridge.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
English,
en,
Base,
);
Expand Down Expand Up @@ -466,7 +467,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "dsbridgedemo/dsbridgedemo-Bridging-Header.h";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 5.0;
};
name = Debug;
};
Expand All @@ -483,7 +484,7 @@
PRODUCT_BUNDLE_IDENTIFIER = wendu.dsbridgedemo.xx;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "dsbridgedemo/dsbridgedemo-Bridging-Header.h";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 5.0;
};
name = Release;
};
Expand Down
6 changes: 3 additions & 3 deletions dsbridge/DSCallInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#import <Foundation/Foundation.h>

@interface DSCallInfo : NSObject
@property (nullable, nonatomic) NSString* method;
@property (nullable, nonatomic) NSNumber* id;
@property (nullable,nonatomic) NSArray * args;
@property (nullable, nonatomic) NSString *method;
@property (nullable, nonatomic) NSNumber *id;
@property (nullable, nonatomic) NSArray *args;
@end
47 changes: 32 additions & 15 deletions dsbridge/DWKWebView.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,38 @@

#import <WebKit/WebKit.h>

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

@interface DWKWebView : WKWebView <WKUIDelegate>

@property (nullable, nonatomic, weak) id <WKUIDelegate> DSUIDelegate;
/// Delegate
@property (nullable, nonatomic, weak) id<WKUIDelegate> DSUIDelegate;

- (void)loadUrl: (NSString * _Nonnull) url;
/// Load web use the url.
/// @param url the url of the web page.
- (void)loadUrl:(NSString *_Nonnull)url;

// Call javascript handler
-(void) callHandler:(NSString * _Nonnull) methodName arguments:(NSArray * _Nullable) args;
-(void) callHandler:(NSString * _Nonnull) methodName completionHandler:(void (^ _Nullable)(id _Nullable value))completionHandler;
-(void) callHandler:(NSString * _Nonnull) methodName arguments:(NSArray * _Nullable) args completionHandler:(void (^ _Nullable)(id _Nullable value))completionHandler;
/// Call javascript method
/// @param methodName js called method name.
/// @param args the method's args.
- (void)callHandler:(NSString *_Nonnull)methodName arguments:(NSArray *_Nullable)args;

/// Call javascript method
/// @param methodName js called method name.
/// @param completionHandler completion handler block.
- (void)callHandler:(NSString *_Nonnull)methodName completionHandler:(void (^_Nullable)(id _Nullable value))completionHandler;


/// Call javascript method
/// @param methodName js called method name.
/// @param args the method's args.
/// @param completionHandler completion handler block.
- (void)callHandler:(NSString *_Nonnull)methodName
arguments:(NSArray *_Nullable)args
completionHandler:(void (^_Nullable)(id _Nullable value))completionHandler;

// set a listener for javascript closing the current page.
- (void)setJavascriptCloseWindowListener:(void(^_Nullable)(void))callback;
- (void)setJavascriptCloseWindowListener:(void (^_Nullable)(void))callback;

/**
* Add a Javascript Object to dsBridge with namespace.
Expand All @@ -31,24 +48,24 @@ typedef void (^JSCallback)(NSString * _Nullable result,BOOL complete);
* @param namespace
* if empty, the object have no namespace.
**/
- (void)addJavascriptObject:(id _Nullable ) object namespace:(NSString * _Nullable) namespace;
- (void)addJavascriptObject:(id _Nullable)object namespace:(NSString *_Nullable)namespace;

// Remove the Javascript Object with the supplied namespace
- (void)removeJavascriptObject:(NSString * _Nullable) namespace;
- (void)removeJavascriptObject:(NSString *_Nullable)namespace;

// Test whether the handler exist in javascript
- (void) hasJavascriptMethod:(NSString * _Nonnull) handlerName methodExistCallback:(void(^ _Nullable)(bool exist))callback;
- (void)hasJavascriptMethod:(NSString *_Nonnull)handlerName methodExistCallback:(void (^_Nullable)(bool exist))callback;

// Set debug mode. if in debug mode, some errors will be prompted by a dialog
// and the exception caused by the native handlers will not be captured.
- (void) setDebugMode:(bool) debug;
- (void)setDebugMode:(bool)debug;

- (void) disableJavascriptDialogBlock:(bool) disable;
- (void)disableJavascriptDialogBlock:(bool)disable;

// custom the label text of javascript dialog that includes alert/confirm/prompt
- (void) customJavascriptDialogLabelTitles:(NSDictionary*_Nullable) dic;
- (void)customJavascriptDialogLabelTitles:(NSDictionary *_Nullable)dic;

// private method, the developer shoudn't call this method
- (id _Nullable ) onMessage:(NSDictionary *_Nonnull) msg type:(int) type;
- (id _Nullable)onMessage:(NSDictionary *_Nonnull)msg type:(int)type;

@end
Loading