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:
I do not know if that makes any sense.
How to thoroughly configure the WKWebView?