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

javascript - iOS 11.3 WKWebView: input.focus() doesn't do anything - Stack Overflow

programmeradmin2浏览0评论

Since updating to iOS 11.3, HTML input fields cannot be focused anymore using JavaScript .focus() except when the function call is immediately following a touch interaction (it used to be possible to use .focus() at any time). Is there a known workaround?

Since updating to iOS 11.3, HTML input fields cannot be focused anymore using JavaScript .focus() except when the function call is immediately following a touch interaction (it used to be possible to use .focus() at any time). Is there a known workaround?

Share Improve this question asked Mar 31, 2018 at 12:40 user8391200user8391200
Add a ment  | 

5 Answers 5

Reset to default 3

Adding click function to the element to invoke focus function just works fine. Or any better solution?

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

call blur() before focus().....

Here's the code we currently use in our app development framework to make input.focus() work. Confirmed to be working and accepted in the App Store by Apple:

Class class = NSClassFromString(@"WKContentView");
NSOperatingSystemVersion iOS_11_3_0 = (NSOperatingSystemVersion){11, 3, 0};
NSOperatingSystemVersion iOS_12_2_0 = (NSOperatingSystemVersion){12, 2, 0};
NSOperatingSystemVersion iOS_13_0_0 = (NSOperatingSystemVersion){13, 0, 0};

if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion: iOS_13_0_0]) {
    SEL selector = sel_getUid("_elementDidFocus:userIsInteracting:blurPreviousNode:activityStateChanges: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 if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion: iOS_12_2_0]) {
    SEL selector = sel_getUid("_elementDidFocus: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 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);
}

Seems like they have been pushed some updates to the repo:

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

So make sure you remove cordova ios platform, add the latest version of the wkwebview: cordova-plugin-ionic-webview 1.1.19 and than add npm install and add platform ios. I've just tested it works like a charm.

Instead of relying on a terrible hack like swizzling a private method in an Apple Framework, the best you can probably do is file this as a bug with Apple.

Do a Spotlight search for "Feedback Assistant.app" and log in with your Apple Dev credentials, then file a ticket against iOS WebKit.

发布评论

评论列表(0)

  1. 暂无评论