Skip to content

Commit 78ae011

Browse files
authored
Merge pull request #1 from marcuswestin/master
update
2 parents c6eac77 + 903ec06 commit 78ae011

File tree

74 files changed

+3544
-706
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+3544
-706
lines changed

.github/ISSUE_TEMPLATE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# WVJB Bug Report
2+
3+
Thanks for reporting an issue with WebViewJavascriptBridge.
4+
5+
### Do these 4 things and I will fix your problem!
6+
7+
1. Go to https://github.com/marcuswestin/WebViewJavascriptBridge and click Fork.
8+
2. Clone your fork, `cd` into it and run `make test`. All tests should pass!
9+
3. Edit `Tests/WebViewJavascriptBridgeTests/BridgeTests.m` and create a new, failing test which demostrates your issue.
10+
4. Create a pull request for https://github.com/marcuswestin/WebViewJavascriptBridge
11+
12+
#### That's it!
13+
14+
I will take it from there and promise that I'll fix your problem ASAP.
15+
16+
#### If you don't do this then I can't help you!
17+
18+
And I probably won't :)
19+
20+
Cheers,
21+
@marcuswestin

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## Before your create your PR:
2+
3+
#### Please add tests for any new or changed functionality!
4+
5+
1. Edit `Tests/WebViewJavascriptBridgeTests/BridgeTests.m`
6+
2. Create a new test which demostrates your changes.
7+
3. Run `make test` and make sure your test is passing
8+
4. That's it!
9+
10+
#### Thanks for improving WebViewJavascriptBridge!
11+
12+
Cheers,
13+
@marcuswestin

.travis.yml

Lines changed: 0 additions & 6 deletions
This file was deleted.

Changelog

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,33 @@ Release Checklist
1313
Version History
1414
---------------
1515

16-
Intended v5.0.0
16+
v5.1.1
17+
+ Swift unit tests and examples
18+
+ Implement removeHandler
19+
20+
v5.1.0
21+
+ A single instantiation function for all webview types.
22+
+ Improved test runner.
23+
+ New instructions & templates for github issues and pull requests.
24+
25+
v5.0.5
26+
+ Run all tests for both UIWebView and WKWebView webviews/bridges.
27+
+ Allow for calling handlers from JS with just a handler name and responseCallback function (#184).
28+
29+
v5.0.3 & v5.0.4
30+
+ Recalled builds :)
31+
32+
v5.0.2
33+
+ Fix bug that could cause a crash if the webview loads a new page immediately after JS sends a message.
34+
35+
v5.0.1
1736
+ Removed `WebViewJavascriptBridge -reset`. It should never have been exposed as a public API.
1837
+ Fixed compilation in C99 mode
19-
20-
Next version
21-
+ Pretty-print json in log messages
38+
+ Inline JS source code. WVJB no longer requires `WebViewJavascriptBridge.js.txt` to be included as a resource.
39+
+ Automated testing: see `make test`
40+
+ Added Makefile with common commands
41+
+ Significantly simplified and improved wvjb load detection
42+
+ Simplify API by focusing on explicitly named handlers instead of a default handler and plain `send`.
2243

2344
v4.1.4
2445
+ Improve how WVJB handles the case when there is no ObjC handler for a message received from js.

Example Apps/ExampleApp-OSX.xcodeproj/project.pbxproj

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@
164164
2C136A1917641106004C7401 /* Project object */ = {
165165
isa = PBXProject;
166166
attributes = {
167-
LastUpgradeCheck = 0510;
167+
LastUpgradeCheck = 0810;
168168
ORGANIZATIONNAME = "Marcus Westin";
169169
};
170170
buildConfigurationList = 2C136A1C17641106004C7401 /* Build configuration list for PBXProject "ExampleApp-OSX" */;
@@ -241,15 +241,22 @@
241241
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
242242
CLANG_CXX_LIBRARY = "libc++";
243243
CLANG_ENABLE_OBJC_ARC = YES;
244+
CLANG_WARN_BOOL_CONVERSION = YES;
244245
CLANG_WARN_CONSTANT_CONVERSION = YES;
245246
CLANG_WARN_EMPTY_BODY = YES;
246247
CLANG_WARN_ENUM_CONVERSION = YES;
248+
CLANG_WARN_INFINITE_RECURSION = YES;
247249
CLANG_WARN_INT_CONVERSION = YES;
250+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
251+
CLANG_WARN_UNREACHABLE_CODE = YES;
248252
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
249253
COPY_PHASE_STRIP = NO;
254+
ENABLE_STRICT_OBJC_MSGSEND = YES;
255+
ENABLE_TESTABILITY = YES;
250256
GCC_C_LANGUAGE_STANDARD = gnu99;
251257
GCC_DYNAMIC_NO_PIC = NO;
252258
GCC_ENABLE_OBJC_EXCEPTIONS = YES;
259+
GCC_NO_COMMON_BLOCKS = YES;
253260
GCC_OPTIMIZATION_LEVEL = 0;
254261
GCC_PREPROCESSOR_DEFINITIONS = (
255262
"DEBUG=1",
@@ -258,7 +265,9 @@
258265
GCC_SYMBOLS_PRIVATE_EXTERN = NO;
259266
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
260267
GCC_WARN_ABOUT_RETURN_TYPE = YES;
268+
GCC_WARN_UNDECLARED_SELECTOR = YES;
261269
GCC_WARN_UNINITIALIZED_AUTOS = YES;
270+
GCC_WARN_UNUSED_FUNCTION = YES;
262271
GCC_WARN_UNUSED_VARIABLE = YES;
263272
MACOSX_DEPLOYMENT_TARGET = 10.8;
264273
ONLY_ACTIVE_ARCH = YES;
@@ -273,18 +282,26 @@
273282
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
274283
CLANG_CXX_LIBRARY = "libc++";
275284
CLANG_ENABLE_OBJC_ARC = YES;
285+
CLANG_WARN_BOOL_CONVERSION = YES;
276286
CLANG_WARN_CONSTANT_CONVERSION = YES;
277287
CLANG_WARN_EMPTY_BODY = YES;
278288
CLANG_WARN_ENUM_CONVERSION = YES;
289+
CLANG_WARN_INFINITE_RECURSION = YES;
279290
CLANG_WARN_INT_CONVERSION = YES;
291+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
292+
CLANG_WARN_UNREACHABLE_CODE = YES;
280293
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
281294
COPY_PHASE_STRIP = YES;
282295
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
296+
ENABLE_STRICT_OBJC_MSGSEND = YES;
283297
GCC_C_LANGUAGE_STANDARD = gnu99;
284298
GCC_ENABLE_OBJC_EXCEPTIONS = YES;
299+
GCC_NO_COMMON_BLOCKS = YES;
285300
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
286301
GCC_WARN_ABOUT_RETURN_TYPE = YES;
302+
GCC_WARN_UNDECLARED_SELECTOR = YES;
287303
GCC_WARN_UNINITIALIZED_AUTOS = YES;
304+
GCC_WARN_UNUSED_FUNCTION = YES;
288305
GCC_WARN_UNUSED_VARIABLE = YES;
289306
MACOSX_DEPLOYMENT_TARGET = 10.8;
290307
SDKROOT = macosx;
@@ -299,6 +316,7 @@
299316
GCC_PREFIX_HEADER = "ExampleApp-OSX/ExampleApp-OSX-Prefix.pch";
300317
INFOPLIST_FILE = "ExampleApp-OSX/ExampleApp-OSX-Info.plist";
301318
MACOSX_DEPLOYMENT_TARGET = 10.7;
319+
PRODUCT_BUNDLE_IDENTIFIER = "WebViewJavascriptBridge.$(PRODUCT_NAME:rfc1034identifier)";
302320
PRODUCT_NAME = "$(TARGET_NAME)";
303321
WRAPPER_EXTENSION = app;
304322
};
@@ -312,6 +330,7 @@
312330
GCC_PREFIX_HEADER = "ExampleApp-OSX/ExampleApp-OSX-Prefix.pch";
313331
INFOPLIST_FILE = "ExampleApp-OSX/ExampleApp-OSX-Info.plist";
314332
MACOSX_DEPLOYMENT_TARGET = 10.7;
333+
PRODUCT_BUNDLE_IDENTIFIER = "WebViewJavascriptBridge.$(PRODUCT_NAME:rfc1034identifier)";
315334
PRODUCT_NAME = "$(TARGET_NAME)";
316335
WRAPPER_EXTENSION = app;
317336
};

Example Apps/ExampleApp-OSX/AppDelegate.m

Lines changed: 11 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@
99
#import "AppDelegate.h"
1010
#import <WebKit/WebKit.h>
1111
#import "WebViewJavascriptBridge.h"
12-
#import "WKWebViewJavascriptBridge.h"
1312

1413
@implementation AppDelegate {
1514
WebView* _webView;
1615
WKWebView *_WKWebView;
1716
WebViewJavascriptBridge* _bridge;
18-
WKWebViewJavascriptBridge* _WKBridge;
17+
WebViewJavascriptBridge* _WKBridge;
1918
NSView* _WKWebViewWrapper;
2019
}
2120

@@ -28,38 +27,24 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
2827

2928
- (void)_configureWebview {
3029
// Create Bridge
31-
_bridge = [WebViewJavascriptBridge bridgeForWebView:_webView handler:^(id data, WVJBResponseCallback responseCallback) {
32-
NSLog(@"ObjC received message from JS: %@", data);
33-
responseCallback(@"Response for message from ObjC");
34-
}];
30+
_bridge = [WebViewJavascriptBridge bridgeForWebView:_webView];
3531

3632
[_bridge registerHandler:@"testObjcCallback" handler:^(id data, WVJBResponseCallback responseCallback) {
3733
NSLog(@"testObjcCallback called: %@", data);
3834
responseCallback(@"Response from testObjcCallback");
3935
}];
4036

41-
[_bridge send:@"A string sent from ObjC before Webview has loaded." responseCallback:^(id responseData) {
42-
NSLog(@"objc got response! %@", responseData);
43-
}];
44-
4537
[_bridge callHandler:@"testJavascriptHandler" data:@{ @"foo":@"before ready" }];
4638

4739
// Create Buttons
48-
NSButton *messageButton = [[NSButton alloc] initWithFrame:NSMakeRect(5, 0, 120, 40)];
49-
[messageButton setTitle:@"Send message"];
50-
[messageButton setBezelStyle:NSRoundedBezelStyle];
51-
[messageButton setTarget:self];
52-
[messageButton setAction:@selector(_sendMessage)];
53-
[_webView addSubview:messageButton];
54-
55-
NSButton *callbackButton = [[NSButton alloc] initWithFrame:NSMakeRect(120, 0, 120, 40)];
40+
NSButton *callbackButton = [[NSButton alloc] initWithFrame:NSMakeRect(5, 0, 120, 40)];
5641
[callbackButton setTitle:@"Call handler"];
5742
[callbackButton setBezelStyle:NSRoundedBezelStyle];
5843
[callbackButton setTarget:self];
5944
[callbackButton setAction:@selector(_callHandler)];
6045
[_webView addSubview:callbackButton];
6146

62-
NSButton *webViewToggleButton = [[NSButton alloc] initWithFrame:NSMakeRect(235, 0, 180, 40)];
47+
NSButton *webViewToggleButton = [[NSButton alloc] initWithFrame:NSMakeRect(120, 0, 180, 40)];
6348
[webViewToggleButton setTitle:@"Switch to WKWebView"];
6449
[webViewToggleButton setBezelStyle:NSRoundedBezelStyle];
6550
[webViewToggleButton setTarget:self];
@@ -70,44 +55,31 @@ - (void)_configureWebview {
7055
// Load Page
7156
NSString* htmlPath = [[NSBundle mainBundle] pathForResource:@"ExampleApp" ofType:@"html"];
7257
NSString* html = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];
73-
[[_webView mainFrame] loadHTMLString:html baseURL:nil];
58+
NSURL *baseURL = [NSURL fileURLWithPath:htmlPath];
59+
[[_webView mainFrame] loadHTMLString:html baseURL: baseURL];
7460
}
7561

7662

7763
- (void)_configureWKWebview {
7864
// Create Bridge
79-
_WKBridge = [WKWebViewJavascriptBridge bridgeForWebView:_WKWebView handler:^(id data, WVJBResponseCallback responseCallback) {
80-
NSLog(@"ObjC received message from JS: %@", data);
81-
responseCallback(@"Response for message from ObjC");
82-
}];
65+
_WKBridge = [WebViewJavascriptBridge bridgeForWebView:_WKWebView];
8366

8467
[_WKBridge registerHandler:@"testObjcCallback" handler:^(id data, WVJBResponseCallback responseCallback) {
8568
NSLog(@"testObjcCallback called: %@", data);
8669
responseCallback(@"Response from testObjcCallback");
8770
}];
8871

89-
[_WKBridge send:@"A string sent from ObjC before Webview has loaded." responseCallback:^(id responseData) {
90-
NSLog(@"objc got response! %@", responseData);
91-
}];
92-
9372
[_WKBridge callHandler:@"testJavascriptHandler" data:@{ @"foo":@"before ready" }];
9473

9574
// Create Buttons
96-
NSButton *messageButton = [[NSButton alloc] initWithFrame:NSMakeRect(5, 0, 120, 40)];
97-
[messageButton setTitle:@"Send message"];
98-
[messageButton setBezelStyle:NSRoundedBezelStyle];
99-
[messageButton setTarget:self];
100-
[messageButton setAction:@selector(_WKSendMessage)];
101-
[_WKWebView addSubview:messageButton];
102-
103-
NSButton *callbackButton = [[NSButton alloc] initWithFrame:NSMakeRect(120, 0, 120, 40)];
75+
NSButton *callbackButton = [[NSButton alloc] initWithFrame:NSMakeRect(5, 0, 120, 40)];
10476
[callbackButton setTitle:@"Call handler"];
10577
[callbackButton setBezelStyle:NSRoundedBezelStyle];
10678
[callbackButton setTarget:self];
10779
[callbackButton setAction:@selector(_WKCallHandler)];
10880
[_WKWebView addSubview:callbackButton];
10981

110-
NSButton *webViewToggleButton = [[NSButton alloc] initWithFrame:NSMakeRect(235, 0, 180, 40)];
82+
NSButton *webViewToggleButton = [[NSButton alloc] initWithFrame:NSMakeRect(120, 0, 180, 40)];
11183
[webViewToggleButton setTitle:@"Switch to WebView"];
11284
[webViewToggleButton setBezelStyle:NSRoundedBezelStyle];
11385
[webViewToggleButton setTarget:self];
@@ -117,33 +89,22 @@ - (void)_configureWKWebview {
11789
// Load Page
11890
NSString* htmlPath = [[NSBundle mainBundle] pathForResource:@"ExampleApp" ofType:@"html"];
11991
NSString* html = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];
120-
[_WKWebView loadHTMLString:html baseURL:nil];
92+
NSURL *baseURL = [NSURL fileURLWithPath:htmlPath];
93+
[_WKWebView loadHTMLString:html baseURL:baseURL];
12194
}
12295

12396
-(void)_toggleExample {
12497
_WKWebView.hidden = !_WKWebView.isHidden;
12598
_webView.hidden = !_webView.isHidden;
12699
}
127100

128-
- (void)_sendMessage {
129-
[_bridge send:@"A string sent from ObjC to JS" responseCallback:^(id response) {
130-
NSLog(@"sendMessage got response: %@", response);
131-
}];
132-
}
133-
134101
- (void)_callHandler {
135102
id data = @{ @"greetingFromObjC": @"Hi there, JS!" };
136103
[_bridge callHandler:@"testJavascriptHandler" data:data responseCallback:^(id response) {
137104
NSLog(@"testJavascriptHandler responded: %@", response);
138105
}];
139106
}
140107

141-
- (void)_WKSendMessage {
142-
[_WKBridge send:@"A string sent from ObjC to JS" responseCallback:^(id response) {
143-
NSLog(@"sendMessage got response: %@", response);
144-
}];
145-
}
146-
147108
- (void)_WKCallHandler {
148109
id data = @{ @"greetingFromObjC": @"Hi there, JS!" };
149110
[_WKBridge callHandler:@"testJavascriptHandler" data:data responseCallback:^(id response) {

Example Apps/ExampleApp-OSX/ExampleApp-OSX-Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<key>CFBundleIconFile</key>
1010
<string></string>
1111
<key>CFBundleIdentifier</key>
12-
<string>WebViewJavascriptBridge.$(PRODUCT_NAME:rfc1034identifier)</string>
12+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
1313
<key>CFBundleInfoDictionaryVersion</key>
1414
<string>6.0</string>
1515
<key>CFBundleName</key>

0 commit comments

Comments
 (0)