Skip to content

Commit f7e7ea6

Browse files
author
Önder Ceylan
committed
* Initial commit for the fix
1 parent e44a6ce commit f7e7ea6

File tree

5 files changed

+114
-0
lines changed

5 files changed

+114
-0
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# cordova-plugin-wkwebviewx-inputfocusfix
2+
Cordova plugin for fixing auto focus issue of html elements on WKWebView.
3+
4+
Currently WKWebView doesn't focus on elements on js .focus() calls. This plugin is extension of CDVWKWebViewEngine class which does ugly swizzling for the focus. Original idea is on: https://github.com/Telerik-Verified-Plugins/WKWebView/commit/04e8296adeb61f289f9c698045c19b62d080c7e3#L609-L620
5+
6+
## Installation
7+
8+
Install the plugin by running:
9+
```
10+
cordova plugin add https://github.com/onderceylan/cordova-plugin-wkwebviewx-inputfocusfix
11+
```
12+
13+
## Lifetime
14+
15+
The plugin should be out of use once https://github.com/apache/cordova-plugin-wkwebview-engine/pull/37/ and https://github.com/ionic-team/cordova-plugin-wkwebview-engine/pull/171 is merged. Please watch main WKWebView repositories for the merge.

package.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"author": {
3+
"name": "Önder Ceylan <onderceylan@gmail.com>"
4+
},
5+
"license": "MIT",
6+
"repository": {
7+
"type": "git",
8+
"url": "git+https://github.com/onderceylan/cordova-plugin-wkwebview-inputfocusfix"
9+
},
10+
"name": "cordova-plugin-wkwebview-inputfocusfix",
11+
"version": "1.0.0",
12+
"description": "Cordova plugin for fixing auto focus issue of html elements on WKWebView",
13+
"cordova": {
14+
"id": "cordova-plugin-wkwebview-inputfocusfix",
15+
"platforms": [
16+
"ios"
17+
]
18+
},
19+
"keywords": [
20+
"ecosystem:cordova",
21+
"cordova-ios",
22+
"input",
23+
"focus",
24+
"auto-focus",
25+
"wkwebview",
26+
"ionic"
27+
],
28+
"engines": {
29+
"cordovaDependencies": {
30+
">1.0.0": {
31+
"cordova": ">=3.2.0"
32+
}
33+
}
34+
}
35+
}

plugin.xml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version='1.0' encoding='utf-8'?>
2+
<plugin id="cordova-plugin-wkwebview-inputfocusfix" version="1.0.0"
3+
xmlns="http://cordova.apache.org/ns/plugins/1.0"
4+
xmlns="http://apache.org/cordova/ns/plugins/1.0"
5+
xmlns:rim="http://www.blackberry.com/ns/widgets"
6+
xmlns:android="http://schemas.android.com/apk/res/android">
7+
<name>WKWebView Input Focus Fix</name>
8+
<license>MIT</license>
9+
10+
<engines>
11+
<engine name="cordova" version=">=3.2.0" />
12+
</engines>
13+
14+
<platform name="ios">
15+
<config-file parent="/*" target="config.xml">
16+
<feature name="WKWebViewInputFocusFix">
17+
<param name="ios-package" value="WKWebViewInputFocusFix" />
18+
<param name="onload" value="true" />
19+
</feature>
20+
</config-file>
21+
22+
<header-file src="src/ios/CDVWKWebViewEngine+InputFocusFix.h" />
23+
<source-file src="src/ios/CDVWKWebViewEngine+InputFocusFix.m" />
24+
</platform>
25+
</plugin>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#import <objc/runtime.h>
2+
#import <Cordova/NSDictionary+CordovaPreferences.h>
3+
#import "CDVWKWebViewEngine.h"
4+
5+
@interface CDVWKWebViewEngine (InputFocusFix)
6+
+ (void) load;
7+
- (void) swizzleWKContentViewForInputFocus;
8+
- (void) keyboardDisplayDoesNotRequireUserAction;
9+
@end
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#import "CDVWKWebViewEngine+InputFocusFix.h"
2+
3+
@implementation CDVWKWebViewEngine (InputFocusFix)
4+
+ (void) load {
5+
static dispatch_once_t onceToken;
6+
dispatch_once(&onceToken, ^{
7+
CDVWKWebViewEngine *cdvWKWebViewEngine = [[CDVWKWebViewEngine alloc] init];
8+
[cdvWKWebViewEngine swizzleWKContentViewForInputFocus];
9+
});
10+
}
11+
12+
- (void) swizzleWKContentViewForInputFocus {
13+
NSDictionary* settings = self.commandDelegate.settings;
14+
if (![settings cordovaBoolSettingForKey:@"KeyboardDisplayRequiresUserAction" defaultValue:YES]) {
15+
[self keyboardDisplayDoesNotRequireUserAction];
16+
}
17+
}
18+
19+
// https://github.com/Telerik-Verified-Plugins/WKWebView/commit/04e8296adeb61f289f9c698045c19b62d080c7e3
20+
- (void) keyboardDisplayDoesNotRequireUserAction {
21+
SEL sel = sel_getUid("_startAssistingNode:userIsInteracting:blurPreviousNode:userObject:");
22+
Class WKContentView = NSClassFromString(@"WKContentView");
23+
Method method = class_getInstanceMethod(WKContentView, sel);
24+
IMP originalImp = method_getImplementation(method);
25+
IMP imp = imp_implementationWithBlock(^void(id me, void* arg0, BOOL arg1, BOOL arg2, id arg3) {
26+
((void (*)(id, SEL, void*, BOOL, BOOL, id))originalImp)(me, sel, arg0, TRUE, arg2, arg3);
27+
});
28+
method_setImplementation(method, imp);
29+
}
30+
@end

0 commit comments

Comments
 (0)