Reputation: 571
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?
Upvotes: 2
Views: 7296
Reputation: 6666
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);
Upvotes: 5