lundi 27 juin 2016

WkWebView can't open keyboard for input field


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 commented 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.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

Anyone have any ideas?


Aucun commentaire:

Enregistrer un commentaire