iOS 11.3 WKWebView:input.focus()不执行任何操作

问题描述 投票:0回答:4

自从更新到iOS 11.3之后,HTML输入字段不再可以使用JavaScript .focus()进行聚焦,除非函数调用紧随触摸交互之后(过去可以随时使用.focus())。有已知的解决方法吗?

javascript html ios safari wkwebview
4个回答
1
投票

blur()之前呼叫focus() .....


0
投票

似乎它们已被推送到仓库的一些更新:

https://github.com/ionic-team/cordova-plugin-wkwebview-engine/pull/171#issuecomment-377824347

因此请确保您删除cordova ios平台,添加最新版本的wkwebview:cordova-plugin-ionic-webview 1.1.19,然后添加npm install并添加平台ios。我刚刚测试了它的魅力。


0
投票

将click函数添加到元素以调用焦点函数的效果很好。还是更好的解决方案?

<html>
<head>
        <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">

        <meta charset="UTF-8">
        <meta name="apple-mobile-web-app-capable" content="yes" />
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <meta name="format-detection" content="telephone=no, email=no">            
</head>
<body>
    <input id="test" type="text" placeholder="test" />
    <input id="test2" type="text" placeholder="test" />

    <script src="https://cdn.bootcss.com/fastclick/1.0.6/fastclick.js"></script>
    <script>
        window.FastClick.attach(document.body);
        document.getElementById('test').onclick = function(e) {
          // works fine
          e.target.focus();
        };
        document.getElementById('test2').onclick = function(e) {
          // not working
          setTimeout(function() {
            e.target.focus();
          }, 50)
        };
    </script>
</body>
</html>

0
投票

这是我们当前在应用程序开发框架中使用的代码,以使input.focus()正常工作。确认可正常使用并被Apple在App Store中接受:

Class class = NSClassFromString(@"WKContentView");
NSOperatingSystemVersion iOS_11_3_0 = (NSOperatingSystemVersion){11, 3, 0};

if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion: iOS_11_3_0]) {
    SEL selector = sel_getUid("_startAssistingNode:userIsInteracting:blurPreviousNode:changingActivityState:userObject:");
    Method method = class_getInstanceMethod(class, selector);
    IMP original = method_getImplementation(method);
    IMP override = imp_implementationWithBlock(^void(id me, void* arg0, BOOL arg1, BOOL arg2, BOOL arg3, id arg4) {
        ((void (*)(id, SEL, void*, BOOL, BOOL, BOOL, id))original)(me, selector, arg0, TRUE, arg2, arg3, arg4);
    });
    method_setImplementation(method, override);
} else {
    SEL selector = sel_getUid("_startAssistingNode:userIsInteracting:blurPreviousNode:userObject:");
    Method method = class_getInstanceMethod(class, selector);
    IMP original = method_getImplementation(method);
    IMP override = imp_implementationWithBlock(^void(id me, void* arg0, BOOL arg1, BOOL arg2, id arg3) {
        ((void (*)(id, SEL, void*, BOOL, BOOL, id))original)(me, selector, arg0, TRUE, arg2, arg3);
    });
    method_setImplementation(method, override);
}
© www.soinside.com 2019 - 2024. All rights reserved.