简体   繁体   中英

Disable autocomplete in UIWebView/WKWebview

Im working on a simple app that acts as a webview. I need to disable auto complete and several other keyboard functions on anything loaded in the webview. I know how to disable autocomplete per UITextInput in a normal view controller but I don't know weather you can disable autocomplete globally on a webview?

I looked at the documentation for UITextInputTraits Here but could not seem to make anything work.

A final resort would be to disable autocomplete in the HTML tags but that would require a lot of work on the back end.

Is there a way to disable autocomplete globally for the app?

Thanks in advance.

I don't think there is a way to do this with iOS code. But, i might be wrong.

You can try running the below JS in the webview every time you load the web page

var textFields = document.getElementsByTagName('input');

if (textFields) {
    var i;
    for( i = 0; i < textFields.length; i++) {
        var txtField = textFields[i];
        if(txtField) {
            txtField.setAttribute('autocomplete','off');
            txtField.setAttribute('autocorrect','off');
            txtField.setAttribute('autocapitalize','off');
            txtField.setAttribute('spellcheck','false');
        }
    }
}

In my opinion, this should do the job without you having to do the changes in the server :)

Would be interested to know if this worked for you

if you are using a WKWebView, use this function:

func RemoveAutoCompleteFromWebView(webView: WKWebView) {
    let script: String = """
        var textFields = document.getElementsByTagName('input');

        if (textFields) {
            var i;
            for( i = 0; i < textFields.length; i++) {
                var txtField = textFields[i];
                if(txtField) {
                    txtField.setAttribute('autocomplete','off');
                    txtField.setAttribute('autocorrect','off');
                    txtField.setAttribute('autocapitalize','off');
                    txtField.setAttribute('spellcheck','false');
                }
            }
        }
    """
    webView.evaluateJavaScript(script, completionHandler: nil)
}

This will run the JavaScript necessary for disabling autocomplete in the new type of WebView. Just pass in the WKWebView that you want.

I have searched alot for this issue. If you want to totally disable it, then you have to use <textarea> instead of <div contenteditable="true"> and load it in UIWebview . Try the below code:

 <textarea id="content" spellcheck="false" autocorrect="off" autocomplete="off"></textarea>

By using this code, Predictive text and autocomplete both will get off .

Swift 4

func disableAutocomplete() {
    let disableAutocompleteScript: String = """
        var textFields = document.getElementsByTagName('textarea');
        if (textFields) {
            var i;
            for( i = 0; i < textFields.length; i++) {
                var txtField = textFields[i];
                if(txtField) {
                    txtField.setAttribute('autocomplete','off');
                    txtField.setAttribute('autocorrect','off');
                    txtField.setAttribute('autocapitalize','off');
                    txtField.setAttribute('spellcheck','false');
                }
            }
        }
    """
    webView.stringByEvaluatingJavaScript(from: disableAutocompleteScript)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM