Skip to content

Commit f0f9c98

Browse files
committed
There is no need for -initWithDelegate, -init and +javascriptBridge. Just use +javascriptBridgeWithDelegate
1 parent 96eedc6 commit f0f9c98

File tree

2 files changed

+7
-32
lines changed

2 files changed

+7
-32
lines changed

WebViewJavascriptBridge/Classes/WebViewJavascriptBridge.h

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,13 @@
1010

1111
@interface WebViewJavascriptBridge : NSObject <UIWebViewDelegate>
1212

13-
/* Delegate to receive messages from javascript.
14-
* Defined as IBOutlet for Interface Builder assignment */
1513
@property (nonatomic, assign) IBOutlet id <WebViewJavascriptBridgeDelegate> delegate;
1614

17-
/* Init with a predefined delegate */
18-
- (id)initWithDelegate:(id <WebViewJavascriptBridgeDelegate>)delegate;
19-
20-
/* Convenience methods for obtaining a bridge */
21-
+ (id)javascriptBridge;
15+
/* Create a javascript bridge with the given delegate for handling messages */
2216
+ (id)javascriptBridgeWithDelegate:(id <WebViewJavascriptBridgeDelegate>)delegate;
2317

24-
/* Sends message to given webView. You need to integrate javascript bridge into
25-
* this view before by calling WebViewJavascriptBridge#webViewDidFinishLoad: with that view.
26-
*
27-
* You can call this method before calling webViewDidFinishLoad: , then all messages
28-
* will be accumulated in _startupMessageQueue & sended to webView, provided by first
29-
* webViewDidFinishLoad: call.
30-
*/
18+
/* Send a message to the web view. Make sure that this javascript bridge is the delegate
19+
* of the webview before calling this method (see ExampleAppDelegate.m) */
3120
- (void)sendMessage:(NSString *)message toWebView:(UIWebView *)webView;
3221

3322
@end

WebViewJavascriptBridge/Classes/WebViewJavascriptBridge.m

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,11 @@ @implementation WebViewJavascriptBridge
1818
static NSString *CUSTOM_PROTOCOL_SCHEME = @"webviewjavascriptbridge";
1919
static NSString *QUEUE_HAS_MESSAGE = @"queuehasmessage";
2020

21-
- (id)initWithDelegate:(id <WebViewJavascriptBridgeDelegate>)delegate {
22-
if ((self = [super init])) {
23-
self.delegate = delegate;
24-
self.startupMessageQueue = [[[NSMutableArray alloc] init] autorelease];
25-
}
26-
27-
return self;
28-
}
29-
30-
- (id)init {
31-
return [[[WebViewJavascriptBridge alloc] initWithDelegate:nil] autorelease];
32-
}
33-
3421
+ (id)javascriptBridgeWithDelegate:(id <WebViewJavascriptBridgeDelegate>)delegate {
35-
return [[[self alloc] initWithDelegate:delegate] autorelease];
36-
}
37-
38-
+ (id)javascriptBridge {
39-
return [[[self alloc] init] autorelease];
22+
WebViewJavascriptBridge* bridge = [[[WebViewJavascriptBridge alloc] init] autorelease];
23+
bridge.delegate = delegate;
24+
bridge.startupMessageQueue = [[[NSMutableArray alloc] init] autorelease];
25+
return bridge;
4026
}
4127

4228
- (void)dealloc {

0 commit comments

Comments
 (0)