最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - WkWebView can't open keyboard for input field - Stack Overflow

programmeradmin7浏览0评论

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.

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.

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

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 mented out alert.

<!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=".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 trying to make a WkWebView open a keyboard for tel text input programatically after the WkWebView sends the contained webpage a javascript function call.

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.

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

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 mented out alert.

<!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./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

Anyone have any ideas?

Share Improve this question asked Sep 4, 2015 at 22:01 hawkeyecoderhawkeyecoder 5714 silver badges10 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

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

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

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);
发布评论

评论列表(0)

  1. 暂无评论