WkWebView无法打开输入字段的键盘

时间:2022-10-21 20:54:03

I'm trying to make a WkWebView open a keyboard for tel text input programatically after the WkWebView sends the contained webpage a javascript function call.

在WkWebView向包含的网页发送javascript函数调用之后,我正在尝试使用WkWebView以编程方式打开用于tel文本输入的键盘。

After this call activates a certain input (id: activeElementToShowKeyboard), I would like the keyboard to display for the previous tel input (id: numberInput) for efficiency reasons.

在此调用激活某个输入(id:activeElementToShowKeyboard)之后,出于效率原因,我希望键盘显示前一个tel输入(id:numberInput)。

The element never gains focus and opens the keyboard.

该元素永远不会获得焦点并打开键盘。

I understand why a keyboard wouldn't automatically open when an element requests focus, but I would guess there has to be a way to do this.

我理解为什么当一个元素请求焦点时键盘不会自动打开,但我猜想必须有一种方法可以做到这一点。

I've tried (with no luck): click(); focus(); click().focus(); FastClick jGestures

我试过(没有运气):click();焦点();点击()对焦()。 FastClick jGestures

Here's a sample that will run locally. In chrome's developer console you can type in FunctionCalledByWkWebView("text"); and it will work, but on iOS the keyboard will never open in the line under the commented out alert.

这是一个将在本地运行的示例。在chrome的开发者控制台中,您可以输入FunctionCalledByWkWebView(“text”);它会工作,但在iOS上,键盘永远不会在注释掉的警报下的行中打开。

<!DOCTYPE html>
<html lang="en">

<head>
  <title>Test</title>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1, user-scalable=0">
  <meta name="format-detection" content="telephone=no" />
  <script src="https://code.jquery.com/jquery-1.11.3.js"></script>
</head>

<body>
  <div>
    <input type="text" class="active target" />
    <input type="text" class="target" />
    <input type="tel" id="numberInput" />
    <input type="text" class="target" id="activeElementToShowKeyboard" />
  </div>
  <style type="text/css">
    .active {
      background-color: red;
    }
  </style>
  <script type="text/javascript">
    $(".target").focusin(function() {
      $(".active").removeClass("active");
      $(this).addClass("active");
    });

    function FunctionCalledByWkWebView(someText) {
      var $selected = $(".active").removeClass("active");
      $selected.val(someText);
      var divs = $(".target");

      var newActiveElement = divs.eq((divs.index($selected) + 1) % divs.length).addClass("active");

      if ($(newActiveElement).is("#activeElementToShowKeyboard")) {
        //alert("Should bring up keyboard!");
        $("#numberInput").focus();
      }
    }
  </script>
</body>

</html>

I'm targeting all iOS devices, mainly iPod touches (5th / 6th Gen) or iPhone 6. I'm testing on iOS 8.4

我的目标是所有iOS设备,主要是iPod touch(第5代/第6代)或iPhone 6.我在iOS 8.4上进行测试

Anyone have any ideas?

有人有主意吗?

1 个解决方案

#1


4  

WKWebView will only show the keyboard if the focus is initiated by a user. This is filed as a bug here.

如果焦点是由用户启动的,WKWebView将仅显示键盘。这是作为一个bug提交的。

It is possible to solve with swizzling. Will probably be rejected by Apple though.

可以通过调配来解决。可能会被Apple拒绝。

This mitigates the issue:

这减轻了这个问题:

static void (*originalIMP)(id self, SEL _cmd, void* arg0, BOOL arg1, BOOL arg2, id arg3) = NULL;

void interceptIMP (id self, SEL _cmd, void* arg0, BOOL arg1, BOOL arg2, id arg3) {
    originalIMP(self, _cmd, arg0, TRUE, arg2, arg3);
}

Class cls = NSClassFromString(@"WKContentView");
        SEL originalSelector = NSSelectorFromString(@"_startAssistingNode:userIsInteracting:blurPreviousNode:userObject:");

Method originalMethod = class_getInstanceMethod(cls, originalSelector);

IMP impOvverride = (IMP) interceptIMP;

originalIMP = (void *)method_getImplementation(originalMethod);

method_setImplementation(originalMethod, impOvverride);

#1


4  

WKWebView will only show the keyboard if the focus is initiated by a user. This is filed as a bug here.

如果焦点是由用户启动的,WKWebView将仅显示键盘。这是作为一个bug提交的。

It is possible to solve with swizzling. Will probably be rejected by Apple though.

可以通过调配来解决。可能会被Apple拒绝。

This mitigates the issue:

这减轻了这个问题:

static void (*originalIMP)(id self, SEL _cmd, void* arg0, BOOL arg1, BOOL arg2, id arg3) = NULL;

void interceptIMP (id self, SEL _cmd, void* arg0, BOOL arg1, BOOL arg2, id arg3) {
    originalIMP(self, _cmd, arg0, TRUE, arg2, arg3);
}

Class cls = NSClassFromString(@"WKContentView");
        SEL originalSelector = NSSelectorFromString(@"_startAssistingNode:userIsInteracting:blurPreviousNode:userObject:");

Method originalMethod = class_getInstanceMethod(cls, originalSelector);

IMP impOvverride = (IMP) interceptIMP;

originalIMP = (void *)method_getImplementation(originalMethod);

method_setImplementation(originalMethod, impOvverride);