簡體   English   中英

iOS 11.3 WKWebView:input.focus() 不做任何事情

[英]iOS 11.3 WKWebView: input.focus() doesn't do anything

更新到 iOS 11.3 后,HTML 輸入字段不能再使用 JavaScript .focus() 獲得焦點,除非函數調用緊跟在觸摸交互之后(過去可以隨時使用 .focus() )。 有已知的解決方法嗎?

將單擊功能添加到元素以調用焦點功能就可以正常工作。 或者有什么更好的解決辦法?

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

focus()之前調用blur() focus() .....

似乎他們已經向 repo 推送了一些更新:

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。 我剛剛測試過它就像一個魅力。

這是我們當前在應用程序開發框架中使用的代碼,用於使 input.focus() 工作。 已確認可在 App Store 中運行並被 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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM