Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

CB-10376, CB-12037: (ios) Fix bug where WKWebView doesnt respect the KeyboardDisplayRequiresUserAction setting #37

Closed
wants to merge 9 commits into from
Closed
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
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# Cordova WKWebView Engine FORK

This fork has two new features.

1. It honors the `<preference name="KeyboardDisplayRequiresUserAction" value="false" />` preference.

2. You can dynamically set `window.WkWebKit.allowsBackForwardNavigationGestures(true or false)`.

---

<!--
# license: Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
Expand Down Expand Up @@ -89,6 +99,13 @@ In order to allow swiping backwards and forwards in browser history like Safari
<preference name="AllowBackForwardNavigationGestures" value="true" />
```

You can also set this preference dynamically from JavaScript:

```js
window.WkWebView.allowsBackForwardNavigationGestures(true)
window.WkWebView.allowsBackForwardNavigationGestures(false)
```

Limitations
--------

Expand Down
4 changes: 4 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
<clobbers target="cordova.exec" />
</js-module>

<js-module src="src/www/ios/ios-wkwebview.js" name="ios-wkwebview">
<clobbers target="window.WkWebView" />
</js-module>

<config-file target="config.xml" parent="/*">
<feature name="CDVWKWebViewEngine">
<param name="ios-package" value="CDVWKWebViewEngine" />
Expand Down
2 changes: 2 additions & 0 deletions src/ios/CDVWKWebViewEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@

@property (nonatomic, strong, readonly) id <WKUIDelegate> uiDelegate;

- (void)allowsBackForwardNavigationGestures:(CDVInvokedUrlCommand*)command;

@end
30 changes: 30 additions & 0 deletions src/ios/CDVWKWebViewEngine.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Licensed to the Apache Software Foundation (ASF) under one
under the License.
*/

#import <objc/runtime.h>
#import "CDVWKWebViewEngine.h"
#import "CDVWKWebViewUIDelegate.h"
#import "CDVWKProcessPoolFactory.h"
Expand Down Expand Up @@ -118,6 +119,10 @@ - (void)pluginInitialize
[wkWebView.configuration.userContentController addScriptMessageHandler:(id < WKScriptMessageHandler >)self.viewController name:CDV_BRIDGE_NAME];
}

if (![settings cordovaBoolSettingForKey:@"KeyboardDisplayRequiresUserAction" defaultValue:YES]) {
[self keyboardDisplayDoesNotRequireUserAction];
}

[self updateSettings:settings];

// check if content thread has died on resume
Expand All @@ -132,6 +137,18 @@ - (void)pluginInitialize
[self addURLObserver];
}

// https://github.com/Telerik-Verified-Plugins/WKWebView/commit/04e8296adeb61f289f9c698045c19b62d080c7e3
- (void) keyboardDisplayDoesNotRequireUserAction {
SEL sel = sel_getUid("_startAssistingNode:userIsInteracting:blurPreviousNode:userObject:");
Class WKContentView = NSClassFromString(@"WKContentView");
Method method = class_getInstanceMethod(WKContentView, sel);
IMP originalImp = method_getImplementation(method);
IMP imp = imp_implementationWithBlock(^void(id me, void* arg0, BOOL arg1, BOOL arg2, id arg3) {
((void (*)(id, SEL, void*, BOOL, BOOL, id))originalImp)(me, sel, arg0, TRUE, arg2, arg3);
});
method_setImplementation(method, imp);
}

- (void)onReset {
[self addURLObserver];
}
Expand Down Expand Up @@ -457,6 +474,19 @@ - (void) webView: (WKWebView *) webView decidePolicyForNavigationAction: (WKNavi
return decisionHandler(NO);
}

#pragma mark - Plugin interface

- (void)allowsBackForwardNavigationGestures:(CDVInvokedUrlCommand*)command;
{
id value = [command.arguments objectAtIndex:0];
if (!([value isKindOfClass:[NSNumber class]])) {
value = [NSNumber numberWithBool:NO];
}

WKWebView* wkWebView = (WKWebView*)_engineWebView;
wkWebView.allowsBackForwardNavigationGestures = [value boolValue];
}

@end

#pragma mark - CDVWKWeakScriptMessageHandler
Expand Down
2 changes: 1 addition & 1 deletion src/www/ios/ios-wkwebview-exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,4 @@ if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandl
cordova.define("cordova/exec", function(require, exports, module) {
module.exports = execProxy;
});
}
}
9 changes: 9 additions & 0 deletions src/www/ios/ios-wkwebview.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var exec = require('cordova/exec');

const WkWebKit = {
allowsBackForwardNavigationGestures: function(allow) {
exec(null, null, "CDVWKWebViewEngine", "allowsBackForwardNavigationGestures", [allow]);
}
}

module.exports = WkWebKit