Search code examples
iosswiftuiwebviewwkwebview

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.


Solution

  • 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