0

My Android app features a WebView that is used for special purposes, so it has to be thoroughly configured.

I set all the following preferences in the activity featuring the WebView:

    webView.setVerticalScrollBarEnabled(true);
    webView.setHorizontalScrollBarEnabled(true);
    webView.setScrollbarFadingEnabled(false);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.setKeepScreenOn(false);
    webView.getSettings().setUseWideViewPort(false);
    webView.getSettings().setLoadWithOverviewMode(false);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setAllowFileAccess(false);
    webView.getSettings().setAllowFileAccessFromFileURLs(false);
    webView.getSettings().setAppCacheEnabled(true);
    webView.getSettings().setAllowContentAccess(false);
    webView.getSettings().setCacheMode(LOAD_DEFAULT);
    webView.getSettings().setDatabaseEnabled(true);
    webView.getSettings().setDomStorageEnabled(true);
    webView.getSettings().setGeolocationEnabled(true);
    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
    webView.getSettings().setMixedContentMode(MIXED_CONTENT_NEVER_ALLOW);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        webView.getSettings().setSafeBrowsingEnabled(true);
    }
    webView.getSettings().setSupportMultipleWindows(false);
    webView.getSettings().setSupportZoom(true);
    webView.getSettings().setDisplayZoomControls(false);
    webView.getSettings().setAllowUniversalAccessFromFileURLs(false);
    webView.getSettings().setSaveFormData(true);
    webView.getSettings().setLoadsImagesAutomatically(true);
    webView.getSettings().supportZoom();
    CookieManager cookieManager=CookieManager.getInstance();
    cookieManager.setAcceptCookie(true);
    cookieManager.setAcceptThirdPartyCookies(webView,true);
    cookieManager.setAcceptFileSchemeCookies(false);

The above mentioned Android app has an iOS counterpart that has to sport the exact same features, even for the WebView. I am trying to figure out how to configure the WKWebView in the iOS app.

I am dealing with key-value pairs to be correctly set, I see, but it's very different from Android:

        webView!.configuration.preferences.isFraudulentWebsiteWarningEnabled=true
        
        webView!.configuration.preferences.javaScriptCanOpenWindowsAutomatically=false
        webView!.configuration.preferences.setValue(false, forKey: "allowFileAccessFromFileURLs")
        webView!.configuration.preferences.setValue(true, forKey: "textInteractionEnabled")
        webView!.configuration.preferences.setValue(true, forKey: "loadsImagesAutomatically")
       //.setValue(true, forKey: "webSecurityEnabled")
        webView!.configuration.setValue(false, forKey: "allowUniversalAccessFromFileURLs")
        //webView!.configuration.preferences.setValue(true, forKey: "cookieEnabled")
        webView!.configuration.setValue(true, forKey: "allowsInlineMediaPlayback")
        //webConfiguration.setValue(false, forKey: "requiresUserGestureForAudioPlayback")
        webView!.configuration.preferences.setValue(false, forKey: "javaScriptCanAccessClipboard")
        webView!.configuration.preferences.setValue(true, forKey: "safeBrowsingEnabled")
        //webConfiguration.setValue(false, forKey: "fileSystemAccessEnabled")
        //webConfiguration.setValue(true, forKey: "storageAPIEnabled")
        //webConfiguration.setValue(true, forKey: "notificationsEnabled")
        webView!.configuration.preferences.setValue(true, forKey: "javaScriptEnabled")

        //maybe others with webView!.configuration.websiteDataStore.httpCookieStore
       

How you can see some settings are like

webView!.configuration.preferences.javaScriptCanOpenWindowsAutomatically=false

other ones are like

webView!.configuration.preferences.setValue(false, forKey: "allowFileAccessFromFileURLs")

other ones are like

 webView!.configuration.setValue(false, forKey: "allowUniversalAccessFromFileURLs")

but many ones are just not possible in any of the mentioned ways because keys are not present, so a runtime error is issued whatever form I put those settings instructions (some lines are commented not to have the runtime error).

I read also that for some preferences you to have to deal with things like:

webView!.configuration.websiteDataStore.httpCookieStore

or even more complex functions to force storing cookies, like this:

func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {

//special instructions here
...
...

    }

How to find all keys to properly perform the remaining settings? I found those keys here:

https://chromium.googlesource.com/chromium/src/android_webview/glue/+/refs/heads/main/java/src/com/android/webview/chromium/ContentSettingsAdapter.java

I do not know if that makes any sense.

How to thoroughly configure the WKWebView?

P5music
  • 3,197
  • 2
  • 32
  • 81

1 Answers1

0

Seems a lot of questions here in one post, but I will try to correct one thing: usually you don't modify preferences of the WebView, you instead

  1. create a WKPreferences object, modify it the way you like,
  2. create WKWebViewConfiguration object and assign the WKPrefetences to its .preferences property
  3. create WKWebView with the WKWebViewConfiguration from above:
// Step 1
let preferences = WKPreferences()
preferences.javaScriptCanOpenWindowsAutomatically = false

// Step 2
let configuration = WKWebViewConfiguration()
configuration.preferences = preferences

// Step 3
webView = WKWebView(..., configuration: configuration)

btw javaScriptCanOpenWindowsAutomatically is already false by default on iOS.

As for consistency of preferences... they are not and don't have to be consistent at all. You need to understand what you are setting and why.

timbre timbre
  • 12,648
  • 10
  • 46
  • 77