Skip to content

Add Several Convenient Methods for WKWebView/UIWebView/WebView via Ca… #290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 0 additions & 69 deletions Example Apps/ExampleApp-iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
2CA0465C1711AC8E006DEE8B /* ExampleApp.html in Resources */ = {isa = PBXBuildFile; fileRef = 2CA0465B1711AC8D006DEE8B /* ExampleApp.html */; };
2CAB869B1727684300BD9ED1 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 2CAB869A1727684300BD9ED1 /* [email protected] */; };
2CEB3EC01602563600548120 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2CEB3EBF1602563600548120 /* UIKit.framework */; };
6529BCFA17DEACD6F9C2E820 /* Pods_ExampleApp_iOS.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 495C20DD246029C0B2405EC9 /* Pods_ExampleApp_iOS.framework */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -49,9 +48,6 @@
2CEB3EBF1602563600548120 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
2CEB3EC11602563600548120 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
2CEB3EC31602563600548120 /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; };
495C20DD246029C0B2405EC9 /* Pods_ExampleApp_iOS.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_ExampleApp_iOS.framework; sourceTree = BUILT_PRODUCTS_DIR; };
4BCA4425A9554AF93A944458 /* Pods-ExampleApp-iOS.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ExampleApp-iOS.debug.xcconfig"; path = "Pods/Target Support Files/Pods-ExampleApp-iOS/Pods-ExampleApp-iOS.debug.xcconfig"; sourceTree = "<group>"; };
E4F0C608003752F273BB146F /* Pods-ExampleApp-iOS.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ExampleApp-iOS.release.xcconfig"; path = "Pods/Target Support Files/Pods-ExampleApp-iOS/Pods-ExampleApp-iOS.release.xcconfig"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -61,7 +57,6 @@
files = (
0E4E9D4C1A101E0B00043087 /* WebKit.framework in Frameworks */,
2CEB3EC01602563600548120 /* UIKit.framework in Frameworks */,
6529BCFA17DEACD6F9C2E820 /* Pods_ExampleApp_iOS.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -118,7 +113,6 @@
2CA045B617117439006DEE8B /* ExampleApp-iOS */,
2CEB3EBE1602563600548120 /* Frameworks */,
2CEB3EBC1602563600548120 /* Products */,
81A733051B2F9C5795D856E4 /* Pods */,
);
sourceTree = "<group>";
};
Expand All @@ -137,33 +131,20 @@
2CEB3EBF1602563600548120 /* UIKit.framework */,
2CEB3EC11602563600548120 /* Foundation.framework */,
2CEB3EC31602563600548120 /* CoreGraphics.framework */,
495C20DD246029C0B2405EC9 /* Pods_ExampleApp_iOS.framework */,
);
name = Frameworks;
sourceTree = "<group>";
};
81A733051B2F9C5795D856E4 /* Pods */ = {
isa = PBXGroup;
children = (
4BCA4425A9554AF93A944458 /* Pods-ExampleApp-iOS.debug.xcconfig */,
E4F0C608003752F273BB146F /* Pods-ExampleApp-iOS.release.xcconfig */,
);
name = Pods;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
2CEB3EBA1602563600548120 /* ExampleApp-iOS */ = {
isa = PBXNativeTarget;
buildConfigurationList = 2CEB3ED31602563600548120 /* Build configuration list for PBXNativeTarget "ExampleApp-iOS" */;
buildPhases = (
953B195B62A37FA474FD95D5 /* [CP] Check Pods Manifest.lock */,
2CEB3EB71602563600548120 /* Sources */,
2CEB3EB81602563600548120 /* Frameworks */,
2CEB3EB91602563600548120 /* Resources */,
A9D6EE0CEB388638298EBC18 /* [CP] Embed Pods Frameworks */,
7E64B37A61F02883676CA89F /* [CP] Copy Pods Resources */,
);
buildRules = (
);
Expand Down Expand Up @@ -218,54 +199,6 @@
};
/* End PBXResourcesBuildPhase section */

/* Begin PBXShellScriptBuildPhase section */
7E64B37A61F02883676CA89F /* [CP] Copy Pods Resources */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = "[CP] Copy Pods Resources";
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-ExampleApp-iOS/Pods-ExampleApp-iOS-resources.sh\"\n";
showEnvVarsInLog = 0;
};
953B195B62A37FA474FD95D5 /* [CP] Check Pods Manifest.lock */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = "[CP] Check Pods Manifest.lock";
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n";
showEnvVarsInLog = 0;
};
A9D6EE0CEB388638298EBC18 /* [CP] Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = "[CP] Embed Pods Frameworks";
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-ExampleApp-iOS/Pods-ExampleApp-iOS-frameworks.sh\"\n";
showEnvVarsInLog = 0;
};
/* End PBXShellScriptBuildPhase section */

/* Begin PBXSourcesBuildPhase section */
2CEB3EB71602563600548120 /* Sources */ = {
isa = PBXSourcesBuildPhase;
Expand Down Expand Up @@ -373,7 +306,6 @@
};
2CEB3ED41602563600548120 /* Debug */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = 4BCA4425A9554AF93A944458 /* Pods-ExampleApp-iOS.debug.xcconfig */;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
Expand All @@ -391,7 +323,6 @@
};
2CEB3ED51602563600548120 /* Release */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = E4F0C608003752F273BB146F /* Pods-ExampleApp-iOS.release.xcconfig */;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
Expand Down
9 changes: 9 additions & 0 deletions Example Apps/ExampleApp-iOS/ExampleUIWebViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ - (void)viewWillAppear:(BOOL)animated {

UIWebView* webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:webView];
NSLog(@"Is JS Bridge Did Load %@", [webView didJavascriptBridgeLoadOnWeb] ? @"YES" : @"NO");

[WebViewJavascriptBridge enableLogging];

Expand All @@ -35,6 +36,14 @@ - (void)viewWillAppear:(BOOL)animated {

[self renderButtons:webView];
[self loadExamplePage:webView];

__weak typeof(webView) weakWebView = webView;
webView.webViewJavascriptBridgeLoadedBlock = ^{
NSLog(@"JS Bridge Did Load");

__strong typeof(weakWebView) webView = weakWebView;
NSLog(@"Is JS Bridge Did Load %@", [webView didJavascriptBridgeLoadOnWeb] ? @"YES" : @"NO");
};
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
Expand Down
23 changes: 19 additions & 4 deletions Example Apps/ExampleApp-iOS/ExampleWKWebViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
//

#import "ExampleWKWebViewController.h"
#import "WebViewJavascriptBridge.h"
#import "WKWebViewJavascriptBridge.h"

@interface ExampleWKWebViewController ()

@property WebViewJavascriptBridge* bridge;
@property WKWebViewJavascriptBridge* bridge;

@end

Expand All @@ -23,8 +23,13 @@ - (void)viewWillAppear:(BOOL)animated {
WKWebView* webView = [[NSClassFromString(@"WKWebView") alloc] initWithFrame:self.view.bounds];
webView.navigationDelegate = self;
[self.view addSubview:webView];
[WebViewJavascriptBridge enableLogging];
_bridge = [WebViewJavascriptBridge bridgeForWebView:webView];

[webView didJavascriptBridgeLoadOnWeb:^(BOOL didLoad) {
NSLog(@"Is JS Bridge Did Load %@", didLoad ? @"YES" : @"NO");
}];

[WKWebViewJavascriptBridge enableLogging];
_bridge = [WKWebViewJavascriptBridge bridgeForWebView:webView];
[_bridge setWebViewDelegate:self];

[_bridge registerHandler:@"testObjcCallback" handler:^(id data, WVJBResponseCallback responseCallback) {
Expand All @@ -36,6 +41,16 @@ - (void)viewWillAppear:(BOOL)animated {

[self renderButtons:webView];
[self loadExamplePage:webView];

__weak typeof(webView) weakWebView = webView;
webView.webViewJavascriptBridgeLoadedBlock = ^{
NSLog(@"JS Bridge Did Load");

__strong typeof(weakWebView) webView = weakWebView;
[webView didJavascriptBridgeLoadOnWeb:^(BOOL didLoad) {
NSLog(@"Is JS Bridge Did Load %@", didLoad ? @"YES" : @"NO");
}];
};
}

- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
Expand Down
8 changes: 8 additions & 0 deletions WebViewJavascriptBridge/WKWebViewJavascriptBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,12 @@

@end

@interface WKWebView (WKWebViewJavascriptBridge)

@property (nonatomic, copy) void(^webViewJavascriptBridgeLoadedBlock)();

- (void)didJavascriptBridgeLoadOnWeb:(void(^)(BOOL didLoad))block;

@end

#endif
36 changes: 35 additions & 1 deletion WebViewJavascriptBridge/WKWebViewJavascriptBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@


#import "WKWebViewJavascriptBridge.h"
#import <objc/runtime.h>

#if defined supportsWKWebView

Expand Down Expand Up @@ -140,7 +141,8 @@ - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigati

if ([_base isWebViewJavascriptBridgeURL:url]) {
if ([_base isBridgeLoadedURL:url]) {
[_base injectJavascriptFile];
[_base injectJavascriptFile];
!webView.webViewJavascriptBridgeLoadedBlock ?: webView.webViewJavascriptBridgeLoadedBlock();
} else if ([_base isQueueMessageURL:url]) {
[self WKFlushMessageQueue];
} else {
Expand Down Expand Up @@ -193,5 +195,37 @@ - (NSString*) _evaluateJavascript:(NSString*)javascriptCommand {

@end

static void *kJSBridgeDidLoadBlockKey = &kJSBridgeDidLoadBlockKey;

@implementation WKWebView (WKWebViewJavascriptBridge)

- (void)setWebViewJavascriptBridgeLoadedBlock:(void (^)())webViewJavascriptBridgeLoadedBlock
{
objc_setAssociatedObject(self, kJSBridgeDidLoadBlockKey, webViewJavascriptBridgeLoadedBlock, OBJC_ASSOCIATION_COPY);
}

- (void (^)())webViewJavascriptBridgeLoadedBlock
{
id block = objc_getAssociatedObject(self, kJSBridgeDidLoadBlockKey);

return block;
}

- (void)didJavascriptBridgeLoadOnWeb:(void (^)(BOOL))block
{
__block BOOL isLoaded = NO;

[self evaluateJavaScript:@"typeof WebViewJavascriptBridge == \'object\';" completionHandler:^(id _Nullable obj, NSError * _Nullable error) {

if (obj && [obj respondsToSelector:@selector(boolValue)])
{
isLoaded = [obj boolValue];
}

!block ?: block(isLoaded);
}];
}

@end

#endif
9 changes: 9 additions & 0 deletions WebViewJavascriptBridge/WebViewJavascriptBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,12 @@
- (void)disableJavscriptAlertBoxSafetyTimeout;

@end


@interface WVJB_WEBVIEW_TYPE (WebViewJavascriptBridge)

@property (nonatomic, copy) void(^webViewJavascriptBridgeLoadedBlock)();

- (BOOL)didJavascriptBridgeLoadOnWeb;

@end
31 changes: 29 additions & 2 deletions WebViewJavascriptBridge/WebViewJavascriptBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

#import "WebViewJavascriptBridge.h"

#import <objc/runtime.h>
#if defined(supportsWKWebView)
#import "WKWebViewJavascriptBridge.h"
#endif
Expand Down Expand Up @@ -125,7 +125,8 @@ - (void)webView:(WebView *)webView decidePolicyForNavigationAction:(NSDictionary
NSURL *url = [request URL];
if ([_base isWebViewJavascriptBridgeURL:url]) {
if ([_base isBridgeLoadedURL:url]) {
[_base injectJavascriptFile];
[_base injectJavascriptFile];
!webView.webViewJavascriptBridgeLoadedBlock ?: webView.webViewJavascriptBridgeLoadedBlock();
} else if ([_base isQueueMessageURL:url]) {
NSString *messageQueueString = [self _evaluateJavascript:[_base webViewJavascriptFetchQueyCommand]];
[_base flushMessageQueue:messageQueueString];
Expand Down Expand Up @@ -183,6 +184,7 @@ - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)
if ([_base isWebViewJavascriptBridgeURL:url]) {
if ([_base isBridgeLoadedURL:url]) {
[_base injectJavascriptFile];
!webView.webViewJavascriptBridgeLoadedBlock ?: webView.webViewJavascriptBridgeLoadedBlock();
} else if ([_base isQueueMessageURL:url]) {
NSString *messageQueueString = [self _evaluateJavascript:[_base webViewJavascriptFetchQueyCommand]];
[_base flushMessageQueue:messageQueueString];
Expand All @@ -209,3 +211,28 @@ - (void)webViewDidStartLoad:(UIWebView *)webView {
#endif

@end

static void *kJSBridgeDidLoadBlockKey = &kJSBridgeDidLoadBlockKey;

@implementation WVJB_WEBVIEW_TYPE (WebViewJavascriptBridge)

- (void)setWebViewJavascriptBridgeLoadedBlock:(void (^)())webViewJavascriptBridgeLoadedBlock
{
objc_setAssociatedObject(self, kJSBridgeDidLoadBlockKey, webViewJavascriptBridgeLoadedBlock, OBJC_ASSOCIATION_COPY);
}

- (void (^)())webViewJavascriptBridgeLoadedBlock
{
id block = objc_getAssociatedObject(self, kJSBridgeDidLoadBlockKey);

return block;
}

- (BOOL)didJavascriptBridgeLoadOnWeb
{
NSString *result = [self stringByEvaluatingJavaScriptFromString:@"typeof WebViewJavascriptBridge == \'object\';"];

return [result boolValue];
}

@end