@zenkit-dev/cordova-plugin-wkwebview-inputfocusfix 1.0.5-dev.1 → 1.0.6-dev.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE CHANGED
@@ -1,5 +1,6 @@
1
1
  MIT License
2
2
 
3
+ Copyright (c) 2017 Önder Ceylan
3
4
  Copyright (c) 2019 Zenkit
4
5
 
5
6
  Permission is hereby granted, free of charge, to any person obtaining a copy
package/README.md CHANGED
@@ -1,19 +1,21 @@
1
- # CDVWKWebViewEngine InputFocusFix
1
+ # WKWebView InputFocusFix
2
2
 
3
3
  > Cordova plugin for fixing auto focus issue of html elements on WKWebView
4
4
 
5
- This plugin adds support for the `KeyboardDisplayRequiresUserAction` setting when using the `CDVWKWebViewEngine`, by swizzling of the `WKContentView`.
6
- _This plugin works for iOS 13.0, 12.2, 11.x and 10.x_
5
+ This plugin adds support for the `KeyboardDisplayRequiresUserAction` preference to cordova-ios 6+, by swizzling the `WKContentView`.
6
+ Unless the preference is set to `true`, focusing an element from JavaScript (e.g. `element.focus()` or `autofocus`) shows the keyboard.
7
+ _This plugin works for iOS 13 and later._
7
8
 
8
9
  ## Installation
9
10
 
10
11
  Install the plugin by running:
11
12
 
12
13
  ```sh
13
- cordova plugin add github:zenkit/cordova-plugin-wkwebview-inputfocusfix#v1.0.5
14
+ cordova plugin add @zenkit-dev/cordova-plugin-wkwebview-inputfocusfix
14
15
  ```
15
16
 
16
17
  ## Why the fork?
17
18
 
18
19
  The [original plugin](https://github.com/onderceylan/cordova-plugin-wkwebview-inputfocusfix) was archived because it shouldn't be needed anymore.
19
20
  But this seems only to be true when using the `cordova-plugin-ionic-webview` and it is still needed when using the `cordova-plugin-wkwebview-engine` because it will not be [fixed](https://issues.apache.org/jira/browse/CB-12037?focusedCommentId=16538144&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-16538144).
21
+ Since cordova-ios 6 the WKWebView is built in and does not support the preference either ([apache/cordova-ios#972](https://github.com/apache/cordova-ios/issues/972)).
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@zenkit-dev/cordova-plugin-wkwebview-inputfocusfix",
3
3
  "description": "Cordova plugin for fixing auto focus issue of html elements on WKWebView",
4
- "version": "1.0.5-dev.1",
4
+ "version": "1.0.6-dev.0",
5
5
  "author": "Zenkit <info@zenkit.com>",
6
6
  "repository": "https://github.com/zenkit/cordova-plugin-wkwebview-inputfocusfix.git",
7
7
  "license": "MIT",
@@ -19,5 +19,12 @@
19
19
  "auto-focus",
20
20
  "wkwebview",
21
21
  "ionic"
22
- ]
22
+ ],
23
+ "engines": {
24
+ "cordovaDependencies": {
25
+ ">1.0.0": {
26
+ "cordova": ">=3.2.0"
27
+ }
28
+ }
29
+ }
23
30
  }
package/plugin.xml CHANGED
@@ -1,7 +1,6 @@
1
1
  <?xml version='1.0' encoding='utf-8'?>
2
2
  <plugin id="cordova-plugin-wkwebview-inputfocusfix"
3
- version="1.0.5"
4
- xmlns="http://cordova.apache.org/ns/plugins/1.0"
3
+ version="1.0.6-dev.0"
5
4
  xmlns="http://apache.org/cordova/ns/plugins/1.0"
6
5
  xmlns:rim="http://www.blackberry.com/ns/widgets"
7
6
  xmlns:android="http://schemas.android.com/apk/res/android">
@@ -20,7 +19,7 @@
20
19
  </feature>
21
20
  </config-file>
22
21
 
23
- <header-file src="src/ios/CDVWKWebViewEngine+InputFocusFix.h" />
24
- <source-file src="src/ios/CDVWKWebViewEngine+InputFocusFix.m" />
22
+ <header-file src="src/ios/WKWebViewInputFocusFix.h" />
23
+ <source-file src="src/ios/WKWebViewInputFocusFix.m" />
25
24
  </platform>
26
25
  </plugin>
@@ -0,0 +1,4 @@
1
+ #import <Cordova/CDVPlugin.h>
2
+
3
+ @interface WKWebViewInputFocusFix : CDVPlugin
4
+ @end
@@ -0,0 +1,51 @@
1
+ #import "WKWebViewInputFocusFix.h"
2
+ #import <WebKit/WebKit.h>
3
+ #import <objc/runtime.h>
4
+ #if __has_include(<Cordova/CDVSettingsDictionary.h>)
5
+ #import <Cordova/CDVSettingsDictionary.h>
6
+ #else
7
+ #import <Cordova/NSDictionary+CordovaPreferences.h>
8
+ #endif
9
+
10
+ @implementation WKWebViewInputFocusFix
11
+
12
+ - (void) pluginInitialize {
13
+ [super pluginInitialize];
14
+
15
+ if (![self.webView isKindOfClass:[WKWebView class]]) {
16
+ return;
17
+ }
18
+
19
+ // CDVSettingsDictionary on cordova-ios 8+, NSDictionary before
20
+ __auto_type settings = self.commandDelegate.settings;
21
+ if ([settings cordovaBoolSettingForKey:@"KeyboardDisplayRequiresUserAction" defaultValue:NO]) {
22
+ return;
23
+ }
24
+
25
+ // The swizzle is process-wide, plugin instances must not wrap it again.
26
+ static dispatch_once_t onceToken;
27
+ dispatch_once(&onceToken, ^{
28
+ [WKWebViewInputFocusFix keyboardDisplayDoesNotRequireUserAction];
29
+ });
30
+ }
31
+
32
+ // https://github.com/Telerik-Verified-Plugins/WKWebView/commit/04e8296adeb61f289f9c698045c19b62d080c7e3
33
+ // https://stackoverflow.com/a/48623286/3297914
34
+ + (void) keyboardDisplayDoesNotRequireUserAction {
35
+ Class class = NSClassFromString(@"WKContentView");
36
+ SEL selector = sel_getUid("_elementDidFocus:userIsInteracting:blurPreviousNode:activityStateChanges:userObject:");
37
+ Method method = class_getInstanceMethod(class, selector);
38
+ if (method == NULL) {
39
+ NSLog(@"WKWebViewInputFocusFix: WKContentView %@ not found, focus() will not show the keyboard", NSStringFromSelector(selector));
40
+ return;
41
+ }
42
+
43
+ IMP original = method_getImplementation(method);
44
+ // activityStateChanges is an OptionSet<WebCore::ActivityState> (uint16_t), pass it on unchanged
45
+ IMP override = imp_implementationWithBlock(^void(id me, void* arg0, BOOL arg1, BOOL arg2, NSUInteger arg3, id arg4) {
46
+ ((void (*)(id, SEL, void*, BOOL, BOOL, NSUInteger, id))original)(me, selector, arg0, YES, arg2, arg3, arg4);
47
+ });
48
+ method_setImplementation(method, override);
49
+ }
50
+
51
+ @end
@@ -1,12 +0,0 @@
1
- #import <Foundation/Foundation.h>
2
- #import <Cordova/CDVPlugin.h>
3
- #import <WebKit/WebKit.h>
4
-
5
- NS_ASSUME_NONNULL_BEGIN
6
-
7
- @interface WKWebViewInputFocusFix : CDVPlugin<WKScriptMessageHandler, NSURLSessionDelegate>
8
- - (void) keyboardDisplayDoesNotRequireUserAction;
9
-
10
- @end
11
-
12
- NS_ASSUME_NONNULL_END
@@ -1,68 +0,0 @@
1
- #import <objc/runtime.h>
2
- #import "CDVWKWebViewEngine+InputFocusFix.h"
3
- #import <Cordova/CDV.h>
4
-
5
- NS_ASSUME_NONNULL_BEGIN
6
-
7
- @implementation WKWebViewInputFocusFix
8
-
9
- -(void) pluginInitialize {
10
- [super pluginInitialize];
11
-
12
- if (![self.webView isKindOfClass:WKWebView.class])
13
- return;
14
-
15
- [self keyboardDisplayDoesNotRequireUserAction];
16
- }
17
-
18
- // https://github.com/Telerik-Verified-Plugins/WKWebView/commit/04e8296adeb61f289f9c698045c19b62d080c7e3
19
- // https://stackoverflow.com/a/48623286/3297914
20
- - (void) keyboardDisplayDoesNotRequireUserAction {
21
- Class class = NSClassFromString(@"WKContentView");
22
- if (class == nil) {
23
- return;
24
- }
25
- NSOperatingSystemVersion iOS_11_3_0 = (NSOperatingSystemVersion){11, 3, 0};
26
- NSOperatingSystemVersion iOS_12_2_0 = (NSOperatingSystemVersion){12, 2, 0};
27
- NSOperatingSystemVersion iOS_13_0_0 = (NSOperatingSystemVersion){13, 0, 0};
28
- NSOperatingSystemVersion iOS_14_0_0 = (NSOperatingSystemVersion){14, 0, 0};
29
- NSOperatingSystemVersion iOS_15_0_0 = (NSOperatingSystemVersion){15, 0, 0};
30
-
31
- if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion: iOS_11_3_0]) {
32
- char* methodSignature = "_startAssistingNode:userIsInteracting:blurPreviousNode:changingActivityState:userObject:";
33
- if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion: iOS_15_0_0]) {
34
- methodSignature = "_elementDidFocus:userIsInteracting:blurPreviousNode:activityStateChanges:userObject:";
35
- } else if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion: iOS_14_0_0]) {
36
- methodSignature = "_elementDidFocus:userIsInteracting:blurPreviousNode:activityStateChanges:userObject:";
37
- } else if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion: iOS_13_0_0]) {
38
- methodSignature = "_elementDidFocus:userIsInteracting:blurPreviousNode:activityStateChanges:userObject:";
39
- } else if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion: iOS_12_2_0]) {
40
- methodSignature = "_elementDidFocus:userIsInteracting:blurPreviousNode:changingActivityState:userObject:";
41
- }
42
-
43
- SEL selector = sel_getUid(methodSignature);
44
- Method method = class_getInstanceMethod(class, selector);
45
- if (method == NULL) {
46
- return;
47
- }
48
- IMP original = method_getImplementation(method);
49
- IMP override = imp_implementationWithBlock(^void(id me, void* arg0, BOOL arg1, BOOL arg2, BOOL arg3, id arg4) {
50
- ((void (*)(id, SEL, void*, BOOL, BOOL, BOOL, id))original)(me, selector, arg0, TRUE, arg2, arg3, arg4);
51
- });
52
- method_setImplementation(method, override);
53
- } else {
54
- SEL selector = sel_getUid("_startAssistingNode:userIsInteracting:blurPreviousNode:userObject:");
55
- Method method = class_getInstanceMethod(class, selector);
56
- if (method == NULL) {
57
- return;
58
- }
59
- IMP original = method_getImplementation(method);
60
- IMP override = imp_implementationWithBlock(^void(id me, void* arg0, BOOL arg1, BOOL arg2, id arg3) {
61
- ((void (*)(id, SEL, void*, BOOL, BOOL, id))original)(me, selector, arg0, TRUE, arg2, arg3);
62
- });
63
- method_setImplementation(method, override);
64
- }
65
- }
66
- @end
67
-
68
- NS_ASSUME_NONNULL_END