Fixes loadAfterBeforeload on iOS. Resolves #349. (#350)

For both UIWebView and WKWebView implementations on iOS.

<!--
Please make sure the checklist boxes are all checked before submitting the PR. The checklist
is intended as a quick reference, for complete details please see our Contributor Guidelines:

http://cordova.apache.org/contribute/contribute_guidelines.html

Thanks!
-->

### Platforms affected
iOS

### What does this PR do?
Fixes `beforeload` event (introduced by #276) for iOS

### What testing has been done on this change?
Tested both allow & deny loading of URL with both iOS implementations in [test container app](https://github.com/dpa99c/cordova-plugin-inappbrowser-wkwebview-test) (ignore its README).

- To test with UIWebView use options: `beforeload=yes,usewkwebview=no`
- To test with WKWebView use options: `beforeload=yes,usewkwebview=yes`

### Checklist
- [x] [Reported an issue](http://cordova.apache.org/contribute/issues.html) in the JIRA database
- [x] Commit message follows the format: "CB-3232: (android) Fix bug with resolving file paths", where CB-xxxx is the JIRA ID & "android" is the platform affected.
- [x] Added automated test coverage as appropriate for this change.

closes #349
This commit is contained in:
Dave Alden 2018-11-21 18:04:04 +00:00 committed by Jan Piotrowski
parent 3b82c160d9
commit 0fd43ae644
4 changed files with 68 additions and 10 deletions

View File

@ -30,6 +30,7 @@
- (void)injectScriptCode:(CDVInvokedUrlCommand*)command;
- (void)show:(CDVInvokedUrlCommand*)command;
- (void)hide:(CDVInvokedUrlCommand*)command;
- (void)loadAfterBeforeload:(CDVInvokedUrlCommand*)command;
@end

View File

@ -121,5 +121,14 @@
}
}
- (void)loadAfterBeforeload:(CDVInvokedUrlCommand*)command
{
if(self.usewkwebview){
[[CDVWKInAppBrowser getInstance] loadAfterBeforeload:command];
}else{
[[CDVUIInAppBrowser getInstance] loadAfterBeforeload:command];
}
}
@end

View File

@ -27,6 +27,9 @@
@class CDVWKInAppBrowserViewController;
@interface CDVWKInAppBrowser : CDVPlugin {
@private
BOOL _useBeforeload;
BOOL _waitForBeforeload;
}
@property (nonatomic, retain) CDVWKInAppBrowser* instance;
@ -40,6 +43,7 @@
- (void)injectScriptCode:(CDVInvokedUrlCommand*)command;
- (void)show:(CDVInvokedUrlCommand*)command;
- (void)hide:(CDVInvokedUrlCommand*)command;
- (void)loadAfterBeforeload:(CDVInvokedUrlCommand*)command;
@end

View File

@ -60,6 +60,8 @@ static CDVWKInAppBrowser* instance = nil;
instance = self;
_previousStatusBarStyle = -1;
_callbackIdPattern = nil;
_useBeforeload = NO;
_waitForBeforeload = NO;
}
- (id)settingForKey:(NSString*)key
@ -263,6 +265,9 @@ static CDVWKInAppBrowser* instance = nil;
}
}
// use of beforeload event
_useBeforeload = browserOptions.beforeload;
_waitForBeforeload = browserOptions.beforeload;
[self.inAppBrowserViewController navigateTo:url];
[self show:nil withNoAnimate:browserOptions.hidden];
@ -370,6 +375,27 @@ static CDVWKInAppBrowser* instance = nil;
[[UIApplication sharedApplication] openURL:url];
}
- (void)loadAfterBeforeload:(CDVInvokedUrlCommand*)command
{
NSString* urlStr = [command argumentAtIndex:0];
if (!_useBeforeload) {
NSLog(@"unexpected loadAfterBeforeload called without feature beforeload=yes");
}
if (self.inAppBrowserViewController == nil) {
NSLog(@"Tried to invoke loadAfterBeforeload on IAB after it was closed.");
return;
}
if (urlStr == nil) {
NSLog(@"loadAfterBeforeload called with nil argument, ignoring.");
return;
}
NSURL* url = [NSURL URLWithString:urlStr];
_waitForBeforeload = NO;
[self.inAppBrowserViewController navigateTo:url];
}
// This is a helper method for the inject{Script|Style}{Code|File} API calls, which
// provides a consistent method for injecting JavaScript code into the document.
//
@ -480,16 +506,29 @@ static CDVWKInAppBrowser* instance = nil;
* to the InAppBrowser plugin. Care has been taken that other callbacks cannot be triggered, and that no
* other code execution is possible.
*/
- (BOOL)webView:(WKWebView*)theWebView decidePolicyForNavigationAction:(NSURLRequest*)request
{
NSURL* url = request.URL;
BOOL isTopLevelNavigation = [request.URL isEqual:[request mainDocumentURL]];
- (void)webView:(WKWebView *)theWebView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
NSURL* url = navigationAction.request.URL;
NSURL* mainDocumentURL = navigationAction.request.mainDocumentURL;
BOOL isTopLevelNavigation = [url isEqual:mainDocumentURL];
BOOL shouldStart = YES;
// When beforeload=yes, on first URL change, initiate JS callback. Only after the beforeload event, continue.
if (_waitForBeforeload && isTopLevelNavigation) {
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsDictionary:@{@"type":@"beforeload", @"url":[url absoluteString]}];
[pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
//if is an app store link, let the system handle it, otherwise it fails to load it
if ([[ url scheme] isEqualToString:@"itms-appss"] || [[ url scheme] isEqualToString:@"itms-apps"]) {
[theWebView stopLoading];
[self openInSystem:url];
return NO;
shouldStart = NO;
}
else if ((self.callbackId != nil) && isTopLevelNavigation) {
// Send a loadstart event for each top-level navigation (includes redirects).
@ -500,7 +539,15 @@ static CDVWKInAppBrowser* instance = nil;
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
}
return YES;
if (_useBeforeload && isTopLevelNavigation) {
_waitForBeforeload = YES;
}
if(shouldStart){
decisionHandler(WKNavigationActionPolicyAllow);
}else{
decisionHandler(WKNavigationActionPolicyCancel);
}
}
#pragma mark WKScriptMessageHandler delegate
@ -1072,10 +1119,7 @@ BOOL isExiting = FALSE;
self.currentURL = url;
}
[self.navigationDelegate webView:theWebView decidePolicyForNavigationAction:navigationAction.request];
decisionHandler(WKNavigationActionPolicyAllow);
[self.navigationDelegate webView:theWebView decidePolicyForNavigationAction:navigationAction decisionHandler:decisionHandler];
}
- (void)webView:(WKWebView *)theWebView didFinishNavigation:(WKNavigation *)navigation