Xcode 6 Swift WKWebView keyboard settings

后端 未结 2 1142
醉话见心
醉话见心 2021-02-15 05:17

I\'m using a WKWebView to load a website and everything is working fine. However, I do not have access to the keyboard properties the same way I do with a textfield. Can someone

2条回答
  •  旧巷少年郎
    2021-02-15 05:55

    I have an HTML auth page that I display in WKWebView. The autocorrect / suggestion bar appears above the keyboard for the username field. Since I don't control the HTML, I used the following Swift 3 code and javascript to add the autocorrect attribute as mentioned above:

        var webView: WKWebView!
    
    override func loadView() {
    
        let autocorrectJavaScript = "var inputTextElement = document.getElementById('username');"
        + "   if (inputTextElement != null) {"
        + "     var autocorrectAttribute = document.createAttribute('autocorrect');"
        + "     autocorrectAttribute.value = 'off';"
        + "     inputTextElement.setAttributeNode(autocorrectAttribute);"
        + "   }"
        let userScript = WKUserScript(source: autocorrectJavaScript, injectionTime: .atDocumentEnd, forMainFrameOnly: false)
        let webConfiguration = WKWebViewConfiguration()
        webConfiguration.userContentController.addUserScript(userScript)
        webView = WKWebView(frame: .zero, configuration: webConfiguration)
        view = webView
    }
    

    Note: The bar above the keyboard still remains, with other options, just not the suggestions.

提交回复
热议问题