Skip to content

Commit

Permalink
Merge pull request marcuswestin#70 from shuizhongyueming/master
Browse files Browse the repository at this point in the history
create a WebViewJavascriptBridgeDelegate
  • Loading branch information
marcuswestin committed May 22, 2014
2 parents 18b51d2 + 16aa68e commit 49579d6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
15 changes: 15 additions & 0 deletions WebViewJavascriptBridge/WebViewJavascriptBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,28 @@
#define WVJB_WEBVIEW_DELEGATE_TYPE NSObject<UIWebViewDelegate>
#endif

@protocol WebViewJavascriptBridgeDelegate<NSObject>

@optional

// define a method,make the user decide every request from webview should be load except the WVJB request
-(BOOL) WVJB_webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType;


@end

typedef void (^WVJBResponseCallback)(id responseData);
typedef void (^WVJBHandler)(id data, WVJBResponseCallback responseCallback);

@interface WebViewJavascriptBridge : WVJB_WEBVIEW_DELEGATE_TYPE

@property (nonatomic,weak) id <WebViewJavascriptBridgeDelegate> delegate;

+ (instancetype)bridgeForWebView:(WVJB_WEBVIEW_TYPE*)webView handler:(WVJBHandler)handler;
+ (instancetype)bridgeForWebView:(WVJB_WEBVIEW_TYPE*)webView webViewDelegate:(WVJB_WEBVIEW_DELEGATE_TYPE*)webViewDelegate handler:(WVJBHandler)handler;
+ (instancetype)bridgeForWebView:(WVJB_WEBVIEW_TYPE*)webView WVJBDelegate:(id)delegate handler:(WVJBHandler)handler;
+ (instancetype)bridgeForWebView:(WVJB_WEBVIEW_TYPE*)webView webViewDelegate:(WVJB_WEBVIEW_DELEGATE_TYPE*)webViewDelegate handler:(WVJBHandler)handler resourceBundle:(NSBundle*)bundle;
+ (void)enableLogging;

Expand Down
21 changes: 20 additions & 1 deletion WebViewJavascriptBridge/WebViewJavascriptBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ + (instancetype)bridgeForWebView:(WVJB_WEBVIEW_TYPE*)webView webViewDelegate:(WV
WebViewJavascriptBridge* bridge = [[WebViewJavascriptBridge alloc] init];
[bridge _platformSpecificSetup:webView webViewDelegate:webViewDelegate handler:messageHandler resourceBundle:bundle];
[bridge reset];
bridge.delegate = nil;
return bridge;
}

+ (instancetype)bridgeForWebView:(WVJB_WEBVIEW_TYPE*)webView WVJBDelegate:(id)delegate handler:(WVJBHandler)handler {
WebViewJavascriptBridge* bridge = [[WebViewJavascriptBridge alloc] init];
[bridge _platformSpecificSetup:webView webViewDelegate:nil handler:handler];
[bridge reset];
bridge.delegate = delegate;
return bridge;
}

Expand Down Expand Up @@ -387,7 +396,17 @@ - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)
} else if (strongDelegate && [strongDelegate respondsToSelector:@selector(webView:shouldStartLoadWithRequest:navigationType:)]) {
return [strongDelegate webView:webView shouldStartLoadWithRequest:request navigationType:navigationType];
} else {
return YES;
if(self.delegate){
if([self.delegate respondsToSelector:@selector(WVJB_webView:shouldStartLoadWithRequest:navigationType:)]){
return [self.delegate WVJB_webView:webView
shouldStartLoadWithRequest:request
navigationType:navigationType];
}else{
return YES;
}
}else{
return YES;
}
}
}

Expand Down

0 comments on commit 49579d6

Please sign in to comment.