Skip to content

Commit

Permalink
Improved startup message queue for WKWebView
Browse files Browse the repository at this point in the history
  • Loading branch information
lokimeyburg committed Oct 15, 2014
1 parent b535dc2 commit 84bbefc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
4 changes: 2 additions & 2 deletions Example Apps/ExampleApp-iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
0E3214AF19EDF7CF0075E78F /* WebKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0E8082DC19EDD98700479452 /* WebKit.framework */; };
0E8082DB19EDC32300479452 /* WKWebViewJavascriptBridge.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E8082DA19EDC32300479452 /* WKWebViewJavascriptBridge.m */; };
0E8082DD19EDD98700479452 /* WebKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0E8082DC19EDD98700479452 /* WebKit.framework */; settings = {ATTRIBUTES = (Weak, ); }; };
2C1562B5176B9F8400B4AE50 /* WebViewJavascriptBridge.js.txt in Resources */ = {isa = PBXBuildFile; fileRef = 2C1562B4176B9F8400B4AE50 /* WebViewJavascriptBridge.js.txt */; };
2C1562C0176BA63500B4AE50 /* WebViewJavascriptBridge.m in Sources */ = {isa = PBXBuildFile; fileRef = 2C1562A9176B9F6200B4AE50 /* WebViewJavascriptBridge.m */; };
2C45CA2C1884AD520002A4E2 /* ExampleAppViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 2C45CA2B1884AD520002A4E2 /* ExampleAppViewController.m */; };
Expand Down Expand Up @@ -50,7 +50,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
0E8082DD19EDD98700479452 /* WebKit.framework in Frameworks */,
0E3214AF19EDF7CF0075E78F /* WebKit.framework in Frameworks */,
2CEB3EC01602563600548120 /* UIKit.framework in Frameworks */,
2CEB3EC21602563600548120 /* Foundation.framework in Frameworks */,
2CEB3EC41602563600548120 /* CoreGraphics.framework in Frameworks */,
Expand Down
7 changes: 5 additions & 2 deletions WebViewJavascriptBridge/WKWebViewJavascriptBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
// Copyright (c) 2014 Loki Meyburg. All rights reserved.
//

#import <Foundation/Foundation.h>
#if defined(__IPHONE_8_0)

#import <Foundation/Foundation.h>
#define kCustomProtocolScheme @"wvjbscheme"
#define kQueueHasMessage @"__WVJB_QUEUE_MESSAGE__"

Expand All @@ -29,4 +30,6 @@ typedef void (^WVJBHandler)(id data, WVJBResponseCallback responseCallback);
- (void)callHandler:(NSString*)handlerName data:(id)data responseCallback:(WVJBResponseCallback)responseCallback;
- (void)reset;

@end
@end

#endif
20 changes: 13 additions & 7 deletions WebViewJavascriptBridge/WKWebViewJavascriptBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
// Copyright (c) 2014 Loki Meyburg. All rights reserved.
//

#if defined(__IPHONE_8_0)

#import "WKWebViewJavascriptBridge.h"

typedef NSDictionary WVJBMessage;
Expand Down Expand Up @@ -236,28 +238,29 @@ - (void)WKFlushMessageQueue {

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
NSLog(@"1. Finished navigation");
if (webView != _webView) { return; }

_numRequestsLoading--;

if (_numRequestsLoading == 0) {

[webView evaluateJavaScript:@"typeof WebViewJavascriptBridge == \'object\';" completionHandler:^(NSString *result, NSError *error) {
if(![result boolValue]){
NSBundle *bundle = _resourceBundle ? _resourceBundle : [NSBundle mainBundle];
NSString *filePath = [bundle pathForResource:@"WebViewJavascriptBridge.js" ofType:@"txt"];
NSString *js = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
[webView evaluateJavaScript:js completionHandler:nil];

if (_startupMessageQueue) {
for (id queuedMessage in _startupMessageQueue) {
[self _dispatchMessage:queuedMessage];
}
_startupMessageQueue = nil;
}
}
}];
}

if (_startupMessageQueue) {
for (id queuedMessage in _startupMessageQueue) {
[self _dispatchMessage:queuedMessage];
}
_startupMessageQueue = nil;
}

__strong typeof(_webViewDelegate) strongDelegate = _webViewDelegate;
if (strongDelegate && [strongDelegate respondsToSelector:@selector(webView:didFinishNavigation:)]) {
Expand Down Expand Up @@ -317,3 +320,6 @@ - (void)webView:(WKWebView *)webView


@end


#endif

0 comments on commit 84bbefc

Please sign in to comment.