Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
duwen committed Oct 11, 2018
1 parent 06a06f1 commit 1164660
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 224 deletions.
Binary file modified Build/Products/Debug-iphonesimulator/libdsbridge.a
Binary file not shown.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
English| [中文简体](https://github.com/wendux/DSBridge-IOS/blob/master/readme-chs.md)
# DSBridge for IOS

![dsBridge](https://github.com/wendux/DSBridge-IOS/raw/master/img/dsbridge.png)
Expand All @@ -8,9 +9,7 @@

> Modern cross-platform JavaScript bridge, through which you can invoke each other's functions synchronously or asynchronously between JavaScript and native applications.


Chinese documentation [中文文档](https://github.com/wendux/DSBridge-IOS/blob/master/readme-chs.md)

DSBridge-Android:https://github.com/wendux/DSBridge-Android


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.4"
s.version = "3.0.5"
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 @@ -211,118 +211,6 @@
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "dsbridge/DWKWebView.m"
timestampString = "557979526.664736"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "256"
endingLineNumber = "256"
landmarkName = "-call::"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "dsbridge/DWKWebView.m"
timestampString = "557979526.66483"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "251"
endingLineNumber = "251"
landmarkName = "-call::"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "dsbridge/DWKWebView.m"
timestampString = "557979526.664907"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "250"
endingLineNumber = "250"
landmarkName = "-call::"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "dsbridge/DWKWebView.m"
timestampString = "557979526.664975"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "259"
endingLineNumber = "259"
landmarkName = "-call::"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "dsbridge/DWKWebView.m"
timestampString = "557979526.665044"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "248"
endingLineNumber = "248"
landmarkName = "-call::"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "dsbridge/DWKWebView.m"
timestampString = "557979526.665114"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "267"
endingLineNumber = "267"
landmarkName = "-call::"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "dsbridge/DWKWebView.m"
timestampString = "557979526.665176"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "264"
endingLineNumber = "264"
landmarkName = "-call::"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
Expand All @@ -346,7 +234,7 @@
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "dsbridgedemo/JsApiTest.m"
timestampString = "558942264.07623"
timestampString = "560930195.2322249"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "26"
Expand Down
7 changes: 0 additions & 7 deletions dsbridge/DSCallInfo.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
//
// DSCallInfo.h
// dsbridge
//
// Created by du on 2018/1/30.
// Copyright © 2018年 杜文. All rights reserved.
//

#import <Foundation/Foundation.h>

Expand Down
102 changes: 48 additions & 54 deletions dsbridge/DWKWebView.m
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
//
// DSWKwebview.m
//
// Created by wendu on 16/12/28.
// Copyright © 2016 wendu. All rights reserved.
//

#import "DWKWebView.h"
#import "JSBUtil.h"
#import "DSCallInfo.h"
#import "InternalApis.h"
#import <objc/message.h>

@implementation DWKWebView
{
void (^alertHandler)(void);
void (^confirmHandler)(BOOL);
void (^promptHandler)(NSString *);
void(^javascriptCloseWindowListener)(void);
void(^javascriptCloseWindowListener)(void);
int dialogType;
int callId;
bool jsDialogBlock;
Expand Down Expand Up @@ -48,12 +42,12 @@ -(instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration
isPending=false;
isDebug=false;
dialogTextDic=@{};

WKUserScript *script = [[WKUserScript alloc] initWithSource:@"window._dswk=true;"
injectionTime:WKUserScriptInjectionTimeAtDocumentStart
forMainFrameOnly:YES];
[configuration.userContentController addUserScript:script];
self = [super initWithFrame:frame configuration: configuration];
self = [super initWithFrame:frame configuration: configuration];
if (self) {
super.UIDelegate=self;
}
Expand Down Expand Up @@ -83,7 +77,7 @@ - (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSSt
}
}
completionHandler(result);

}else {
if(!jsDialogBlock){
completionHandler(nil);
Expand All @@ -102,11 +96,12 @@ - (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSSt
if(jsDialogBlock){
promptHandler=completionHandler;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:prompt
message:@""
delegate:self
cancelButtonTitle:dialogTextDic[@"promptCancelBtn"]?dialogTextDic[@"promptCancelBtn"]:@"取消"
otherButtonTitles:dialogTextDic[@"promptOkBtn"]?dialogTextDic[@"promptOkBtn"]:@"确定",
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:prompt
message:@""
delegate:self
cancelButtonTitle:dialogTextDic[@"promptCancelBtn"]?dialogTextDic[@"promptCancelBtn"]:@"取消"
otherButtonTitles:dialogTextDic[@"promptOkBtn"]?dialogTextDic[@"promptOkBtn"]:@"确定",
nil];
[alert setAlertViewStyle:UIAlertViewStylePlainTextInput];
txtName = [alert textFieldAtIndex:0];
Expand Down Expand Up @@ -151,8 +146,8 @@ -(void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSStri
if(!jsDialogBlock){
completionHandler(YES);
}
if( self.DSUIDelegate && [self.DSUIDelegate respondsToSelector:
@selector(webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame:completionHandler:)])
if( self.DSUIDelegate&& [self.DSUIDelegate respondsToSelector:
@selector(webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame:completionHandler:)])
{
return[self.DSUIDelegate webView:webView runJavaScriptConfirmPanelWithMessage:message
initiatedByFrame:frame
Expand Down Expand Up @@ -188,25 +183,30 @@ - (void)webViewDidClose:(WKWebView *)webView{
}

- (BOOL)webView:(WKWebView *)webView shouldPreviewElement:(WKPreviewElementInfo *)elementInfo{
if( self.DSUIDelegate && [self.DSUIDelegate respondsToSelector:
@selector(webView:shouldPreviewElement:)]){
return [self.DSUIDelegate webView:webView shouldPreviewElement:elementInfo];
}
if( self.DSUIDelegate
&& [self.DSUIDelegate respondsToSelector:
@selector(webView:shouldPreviewElement:)]){
return [self.DSUIDelegate webView:webView shouldPreviewElement:elementInfo];
}
return NO;
}

- (UIViewController *)webView:(WKWebView *)webView previewingViewControllerForElement:(WKPreviewElementInfo *)elementInfo defaultActions:(NSArray<id<WKPreviewActionItem>> *)previewActions{
if( self.DSUIDelegate && [self.DSUIDelegate respondsToSelector:
@selector(webView:previewingViewControllerForElement:defaultActions:)]){
return [self.DSUIDelegate webView:webView previewingViewControllerForElement:elementInfo defaultActions:previewActions];
if( self.DSUIDelegate &&
[self.DSUIDelegate respondsToSelector:@selector(webView:previewingViewControllerForElement:defaultActions:)]){
return [self.DSUIDelegate
webView:webView
previewingViewControllerForElement:elementInfo
defaultActions:previewActions
];
}
return nil;
}


- (void)webView:(WKWebView *)webView commitPreviewingViewController:(UIViewController *)previewingViewController{
if( self.DSUIDelegate && [self.DSUIDelegate respondsToSelector:
@selector(webView:commitPreviewingViewController:)]){
if( self.DSUIDelegate
&& [self.DSUIDelegate respondsToSelector:@selector(webView:commitPreviewingViewController:)]){
return [self.DSUIDelegate webView:webView commitPreviewingViewController:previewingViewController];
}
}
Expand All @@ -223,7 +223,7 @@ - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)butto
if(buttonIndex==1){
promptHandler([txtName text]);
}else{
promptHandler(@"");
promptHandler(@"");
}
promptHandler=nil;
txtName=nil;
Expand All @@ -246,7 +246,7 @@ - (void) evalJavascript:(int) delay{
-(NSString *)call:(NSString*) method :(NSString*) argStr
{
NSArray *nameStr=[JSBUtil parseNamespace:[method stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
//id JavascriptInterfaceObject= [javaScriptNamespaceInterfaces objectForKey:nameStr[0]];

id JavascriptInterfaceObject=javaScriptNamespaceInterfaces[nameStr[0]];
NSString *error=[NSString stringWithFormat:@"Error! \n Method %@ is not invoked, since there is not a implementation for it",method];
NSMutableDictionary*result =[NSMutableDictionary dictionaryWithDictionary:@{@"code":@-1,@"data":@""}];
Expand All @@ -263,7 +263,6 @@ -(NSString *)call:(NSString*) method :(NSString*) argStr
if(arg==[NSNull null]){
arg=nil;
}
//if(arg)
NSString * cb;
do{
if(args && (cb= args[@"_dscbstub"])){
Expand Down Expand Up @@ -297,20 +296,15 @@ -(NSString *)call:(NSString*) method :(NSString*) argStr
}

};

SuppressPerformSelectorLeakWarning(
[JavascriptInterfaceObject performSelector:selasyn withObject:arg withObject:completionHandler];

);

void(*action)(id,SEL,id,id) = (void(*)(id,SEL,id,id))objc_msgSend;
action(JavascriptInterfaceObject,selasyn,arg,completionHandler);
break;
}
}else if([JavascriptInterfaceObject respondsToSelector:sel]){
id ret;
SuppressPerformSelectorLeakWarning(
ret=[JavascriptInterfaceObject performSelector:sel withObject:arg];
);

id(*action)(id,SEL,id) = (id(*)(id,SEL,id))objc_msgSend;
ret=action(JavascriptInterfaceObject,sel,arg);
[result setValue:@0 forKey:@"code"];
if(ret!=nil){
[result setValue:ret forKey:@"data"];
Expand Down Expand Up @@ -385,7 +379,7 @@ - (void) dispatchJavascriptCall:(DSCallInfo*) info{

- (void) addJavascriptObject:(id)object namespace:(NSString *)namespace{
if(namespace==nil){
namespace=@"";
namespace=@"";
}
if(object!=NULL){
[javaScriptNamespaceInterfaces setObject:object forKey:namespace];
Expand All @@ -409,22 +403,22 @@ - (id)onMessage:(NSDictionary *)msg type:(int)type{
id ret=nil;
switch (type) {
case DSB_API_HASNATIVEMETHOD:
ret= [self hasNativeMethod:msg]?@1:@0;
break;
ret= [self hasNativeMethod:msg]?@1:@0;
break;
case DSB_API_CLOSEPAGE:
[self closePage:msg];
break;
[self closePage:msg];
break;
case DSB_API_RETURNVALUE:
ret=[self returnValue:msg];
break;
ret=[self returnValue:msg];
break;
case DSB_API_DSINIT:
ret=[self dsinit:msg];
break;
ret=[self dsinit:msg];
break;
case DSB_API_DISABLESAFETYALERTBOX:
[self disableJavascriptDialogBlock:[msg[@"disable"] boolValue]];
break;
[self disableJavascriptDialogBlock:[msg[@"disable"] boolValue]];
break;
default:
break;
break;
}
return ret;
}
Expand Down Expand Up @@ -483,9 +477,9 @@ - (void) disableJavascriptDialogBlock:(bool) disable{
}

- (void)hasJavascriptMethod:(NSString *)handlerName methodExistCallback:(void (^)(bool exist))callback{
[self callHandler:@"_hasJavascriptMethod" arguments:@[handlerName] completionHandler:^(NSNumber* _Nullable value) {
callback([value boolValue]);
}];
[self callHandler:@"_hasJavascriptMethod" arguments:@[handlerName] completionHandler:^(NSNumber* _Nullable value) {
callback([value boolValue]);
}];
}

@end
Expand Down
8 changes: 0 additions & 8 deletions dsbridge/InternalApis.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
//
// InternalApis.h
// dsbridge
//
// Created by du on 2018/1/30.
// Copyright © 2018年 杜文. All rights reserved.
//

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

Expand Down
Loading

0 comments on commit 1164660

Please sign in to comment.