Commit 31dc73cf authored by Lorenzo Pichilli's avatar Lorenzo Pichilli

added new webview options

parent b4544c7d
This diff is collapsed.
...@@ -13,9 +13,9 @@ ...@@ -13,9 +13,9 @@
- Added `onLoadResourceCustomScheme` event and `resourceCustomSchemes` option to set custom schemes that WebView must handle to load resources - Added `onLoadResourceCustomScheme` event and `resourceCustomSchemes` option to set custom schemes that WebView must handle to load resources
- Added `onTargetBlank` event and `useOnTargetBlank` option to manage links with `target="_blank"` - Added `onTargetBlank` event and `useOnTargetBlank` option to manage links with `target="_blank"`
- Added `ContentBlocker`, `ContentBlockerTrigger` and `ContentBlockerAction` classes and the `contentBlockers` option that allows to define a set of rules to use to block content in the WebView - Added `ContentBlocker`, `ContentBlockerTrigger` and `ContentBlockerAction` classes and the `contentBlockers` option that allows to define a set of rules to use to block content in the WebView
- Added new WebView options: `minimumFontSize`, `debuggingEnabled`, `preferredContentMode` - Added new WebView options: `minimumFontSize`, `debuggingEnabled`, `preferredContentMode`, `applicationNameForUserAgent`, `incognito`, `cacheEnabled`
- Added new Android WebView options: `allowContentAccess`, `allowFileAccess`, `allowFileAccessFromFileURLs`, `allowUniversalAccessFromFileURLs`, `appCacheEnabled`, `appCachePath`, `blockNetworkImage`, `blockNetworkLoads`, `cacheMode`, `cursiveFontFamily`, `defaultFixedFontSize`, `defaultFontSize`, `defaultTextEncodingName`, `disabledActionModeMenuItems`, `fantasyFontFamily`, `fixedFontFamily`, `forceDark`, `geolocationEnabled`, `layoutAlgorithm`, `loadWithOverviewMode`, `loadsImagesAutomatically`, `minimumLogicalFontSize`, `needInitialFocus`, `offscreenPreRaster`, `sansSerifFontFamily`, `serifFontFamily`, `standardFontFamily` - Added new Android WebView options: `allowContentAccess`, `allowFileAccess`, `allowFileAccessFromFileURLs`, `allowUniversalAccessFromFileURLs`, `appCachePath`, `blockNetworkImage`, `blockNetworkLoads`, `cacheMode`, `cursiveFontFamily`, `defaultFixedFontSize`, `defaultFontSize`, `defaultTextEncodingName`, `disabledActionModeMenuItems`, `fantasyFontFamily`, `fixedFontFamily`, `forceDark`, `geolocationEnabled`, `layoutAlgorithm`, `loadWithOverviewMode`, `loadsImagesAutomatically`, `minimumLogicalFontSize`, `needInitialFocus`, `offscreenPreRaster`, `sansSerifFontFamily`, `serifFontFamily`, `standardFontFamily`, `saveFormData`, `thirdPartyCookiesEnabled`, `hardwareAcceleration`
- Added new iOS WebView options: `applicationNameForUserAgent`, `isFraudulentWebsiteWarningEnabled`, `selectionGranularity`, `dataDetectorTypes` - Added new iOS WebView options: `isFraudulentWebsiteWarningEnabled`, `selectionGranularity`, `dataDetectorTypes`, `sharedCookiesEnabled`
- Added `onGeolocationPermissionsShowPrompt` event and `GeolocationPermissionShowPromptResponse` class (available only for Android) - Added `onGeolocationPermissionsShowPrompt` event and `GeolocationPermissionShowPromptResponse` class (available only for Android)
- Added `startSafeBrowsing`, `setSafeBrowsingWhitelist` and `getSafeBrowsingPrivacyPolicyUrl` methods (available only for Android) - Added `startSafeBrowsing`, `setSafeBrowsingWhitelist` and `getSafeBrowsingPrivacyPolicyUrl` methods (available only for Android)
- Added `clearSslPreferences` and `clearClientCertPreferences` methods (available only for Android) - Added `clearSslPreferences` and `clearClientCertPreferences` methods (available only for Android)
......
...@@ -499,19 +499,28 @@ final public class InAppWebView extends InputAwareWebView { ...@@ -499,19 +499,28 @@ final public class InAppWebView extends InputAwareWebView {
settings.setDatabaseEnabled(options.databaseEnabled); settings.setDatabaseEnabled(options.databaseEnabled);
settings.setDomStorageEnabled(options.domStorageEnabled); settings.setDomStorageEnabled(options.domStorageEnabled);
if (!options.userAgent.isEmpty()) if (options.userAgent != null && !options.userAgent.isEmpty())
settings.setUserAgentString(options.userAgent); settings.setUserAgentString(options.userAgent);
else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
settings.setUserAgentString(WebSettings.getDefaultUserAgent(getContext()));
if (options.applicationNameForUserAgent != null && !options.applicationNameForUserAgent.isEmpty()) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
String userAgent = (options.userAgent != null && !options.userAgent.isEmpty()) ? options.userAgent :WebSettings.getDefaultUserAgent(getContext());
String userAgentWithApplicationName = userAgent + " " + options.applicationNameForUserAgent;
settings.setUserAgentString(userAgentWithApplicationName);
}
}
if (options.clearCache) if (options.clearCache)
clearAllCache(); clearAllCache();
else if (options.clearSessionCache) else if (options.clearSessionCache)
CookieManager.getInstance().removeSessionCookie(); CookieManager.getInstance().removeSessionCookie();
// Enable Thirdparty Cookies on >=Android 5.0 device
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
CookieManager.getInstance().setAcceptThirdPartyCookies(this, true); CookieManager.getInstance().setAcceptThirdPartyCookies(this, options.thirdPartyCookiesEnabled);
settings.setLoadWithOverviewMode(true); settings.setLoadWithOverviewMode(options.loadWithOverviewMode);
settings.setUseWideViewPort(options.useWideViewPort); settings.setUseWideViewPort(options.useWideViewPort);
settings.setSupportZoom(options.supportZoom); settings.setSupportZoom(options.supportZoom);
settings.setTextZoom(options.textZoom); settings.setTextZoom(options.textZoom);
...@@ -528,8 +537,8 @@ final public class InAppWebView extends InputAwareWebView { ...@@ -528,8 +537,8 @@ final public class InAppWebView extends InputAwareWebView {
settings.setAllowFileAccess(options.allowFileAccess); settings.setAllowFileAccess(options.allowFileAccess);
settings.setAllowFileAccessFromFileURLs(options.allowFileAccessFromFileURLs); settings.setAllowFileAccessFromFileURLs(options.allowFileAccessFromFileURLs);
settings.setAllowUniversalAccessFromFileURLs(options.allowUniversalAccessFromFileURLs); settings.setAllowUniversalAccessFromFileURLs(options.allowUniversalAccessFromFileURLs);
settings.setAppCacheEnabled(options.appCacheEnabled); setCacheEnabled(options.cacheEnabled);
if (options.appCachePath != null && !options.appCachePath.isEmpty() && options.appCacheEnabled) if (options.appCachePath != null && !options.appCachePath.isEmpty() && options.cacheEnabled)
settings.setAppCachePath(options.appCachePath); settings.setAppCachePath(options.appCachePath);
settings.setBlockNetworkImage(options.blockNetworkImage); settings.setBlockNetworkImage(options.blockNetworkImage);
settings.setBlockNetworkLoads(options.blockNetworkLoads); settings.setBlockNetworkLoads(options.blockNetworkLoads);
...@@ -548,7 +557,6 @@ final public class InAppWebView extends InputAwareWebView { ...@@ -548,7 +557,6 @@ final public class InAppWebView extends InputAwareWebView {
settings.setGeolocationEnabled(options.geolocationEnabled); settings.setGeolocationEnabled(options.geolocationEnabled);
if (options.layoutAlgorithm != null) if (options.layoutAlgorithm != null)
settings.setLayoutAlgorithm(options.layoutAlgorithm); settings.setLayoutAlgorithm(options.layoutAlgorithm);
settings.setLoadWithOverviewMode(options.loadWithOverviewMode);
settings.setLoadsImagesAutomatically(options.loadsImagesAutomatically); settings.setLoadsImagesAutomatically(options.loadsImagesAutomatically);
settings.setMinimumFontSize(options.minimumFontSize); settings.setMinimumFontSize(options.minimumFontSize);
settings.setMinimumLogicalFontSize(options.minimumLogicalFontSize); settings.setMinimumLogicalFontSize(options.minimumLogicalFontSize);
...@@ -570,6 +578,13 @@ final public class InAppWebView extends InputAwareWebView { ...@@ -570,6 +578,13 @@ final public class InAppWebView extends InputAwareWebView {
break; break;
} }
} }
settings.setSaveFormData(options.saveFormData);
if (options.incognito)
setIncognito(true);
if (options.hardwareAcceleration)
setLayerType(View.LAYER_TYPE_HARDWARE, null);
else
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
contentBlockerHandler.getRuleList().clear(); contentBlockerHandler.getRuleList().clear();
for (Map<String, Map<String, Object>> contentBlocker : options.contentBlockers) { for (Map<String, Map<String, Object>> contentBlocker : options.contentBlockers) {
...@@ -593,6 +608,49 @@ final public class InAppWebView extends InputAwareWebView { ...@@ -593,6 +608,49 @@ final public class InAppWebView extends InputAwareWebView {
}); });
} }
public void setIncognito(boolean enabled) {
WebSettings settings = getSettings();
if (enabled) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
CookieManager.getInstance().removeAllCookies(null);
} else {
CookieManager.getInstance().removeAllCookie();
}
// Disable caching
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
settings.setAppCacheEnabled(false);
clearHistory();
clearCache(true);
// No form data or autofill enabled
clearFormData();
settings.setSavePassword(false);
settings.setSaveFormData(false);
}
else {
settings.setCacheMode(WebSettings.LOAD_DEFAULT);
settings.setAppCacheEnabled(true);
settings.setSavePassword(true);
settings.setSaveFormData(true);
}
}
public void setCacheEnabled(boolean enabled) {
WebSettings settings = getSettings();
if (enabled) {
Context ctx = getContext();
if (ctx != null) {
settings.setAppCachePath(ctx.getCacheDir().getAbsolutePath());
settings.setCacheMode(WebSettings.LOAD_DEFAULT);
settings.setAppCacheEnabled(true);
}
} else {
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
settings.setAppCacheEnabled(false);
}
}
public void loadUrl(String url, MethodChannel.Result result) { public void loadUrl(String url, MethodChannel.Result result) {
if (!url.isEmpty()) { if (!url.isEmpty()) {
loadUrl(url); loadUrl(url);
...@@ -780,11 +838,22 @@ final public class InAppWebView extends InputAwareWebView { ...@@ -780,11 +838,22 @@ final public class InAppWebView extends InputAwareWebView {
if (newOptionsMap.get("userAgent") != null && !options.userAgent.equals(newOptions.userAgent) && !newOptions.userAgent.isEmpty()) if (newOptionsMap.get("userAgent") != null && !options.userAgent.equals(newOptions.userAgent) && !newOptions.userAgent.isEmpty())
settings.setUserAgentString(newOptions.userAgent); settings.setUserAgentString(newOptions.userAgent);
if (newOptionsMap.get("applicationNameForUserAgent") != null && !options.applicationNameForUserAgent.equals(newOptions.applicationNameForUserAgent) && !newOptions.applicationNameForUserAgent.isEmpty()) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
String userAgent = (newOptions.userAgent != null && !newOptions.userAgent.isEmpty()) ? newOptions.userAgent : WebSettings.getDefaultUserAgent(getContext());
String userAgentWithApplicationName = userAgent + " " + options.applicationNameForUserAgent;
settings.setUserAgentString(userAgentWithApplicationName);
}
}
if (newOptionsMap.get("clearCache") != null && newOptions.clearCache) if (newOptionsMap.get("clearCache") != null && newOptions.clearCache)
clearAllCache(); clearAllCache();
else if (newOptionsMap.get("clearSessionCache") != null && newOptions.clearSessionCache) else if (newOptionsMap.get("clearSessionCache") != null && newOptions.clearSessionCache)
CookieManager.getInstance().removeSessionCookie(); CookieManager.getInstance().removeSessionCookie();
if (newOptionsMap.get("thirdPartyCookiesEnabled") != null && options.thirdPartyCookiesEnabled != newOptions.thirdPartyCookiesEnabled && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
CookieManager.getInstance().setAcceptThirdPartyCookies(this, newOptions.thirdPartyCookiesEnabled);
if (newOptionsMap.get("useWideViewPort") != null && options.useWideViewPort != newOptions.useWideViewPort) if (newOptionsMap.get("useWideViewPort") != null && options.useWideViewPort != newOptions.useWideViewPort)
settings.setUseWideViewPort(newOptions.useWideViewPort); settings.setUseWideViewPort(newOptions.useWideViewPort);
...@@ -835,12 +904,11 @@ final public class InAppWebView extends InputAwareWebView { ...@@ -835,12 +904,11 @@ final public class InAppWebView extends InputAwareWebView {
if (newOptionsMap.get("allowUniversalAccessFromFileURLs") != null && options.allowUniversalAccessFromFileURLs != newOptions.allowUniversalAccessFromFileURLs) if (newOptionsMap.get("allowUniversalAccessFromFileURLs") != null && options.allowUniversalAccessFromFileURLs != newOptions.allowUniversalAccessFromFileURLs)
settings.setAllowUniversalAccessFromFileURLs(newOptions.allowUniversalAccessFromFileURLs); settings.setAllowUniversalAccessFromFileURLs(newOptions.allowUniversalAccessFromFileURLs);
if (newOptionsMap.get("appCacheEnabled") != null && options.appCacheEnabled != newOptions.appCacheEnabled) if (newOptionsMap.get("cacheEnabled") != null && options.cacheEnabled != newOptions.cacheEnabled)
settings.setAppCacheEnabled(newOptions.appCacheEnabled); setCacheEnabled(newOptions.cacheEnabled);
if (newOptionsMap.get("appCachePath") != null && !options.appCachePath.equals(newOptions.appCachePath)) if (newOptionsMap.get("appCachePath") != null && !options.appCachePath.equals(newOptions.appCachePath))
if (newOptions.appCacheEnabled) settings.setAppCachePath(newOptions.appCachePath);
settings.setAppCachePath(newOptions.appCachePath);
if (newOptionsMap.get("blockNetworkImage") != null && options.blockNetworkImage != newOptions.blockNetworkImage) if (newOptionsMap.get("blockNetworkImage") != null && options.blockNetworkImage != newOptions.blockNetworkImage)
settings.setBlockNetworkImage(newOptions.blockNetworkImage); settings.setBlockNetworkImage(newOptions.blockNetworkImage);
...@@ -911,6 +979,19 @@ final public class InAppWebView extends InputAwareWebView { ...@@ -911,6 +979,19 @@ final public class InAppWebView extends InputAwareWebView {
if (newOptionsMap.get("standardFontFamily") != null && !options.standardFontFamily.equals(newOptions.standardFontFamily)) if (newOptionsMap.get("standardFontFamily") != null && !options.standardFontFamily.equals(newOptions.standardFontFamily))
settings.setStandardFontFamily(newOptions.standardFontFamily); settings.setStandardFontFamily(newOptions.standardFontFamily);
if (newOptionsMap.get("saveFormData") != null && options.saveFormData != newOptions.saveFormData)
settings.setSaveFormData(newOptions.saveFormData);
if (newOptionsMap.get("incognito") != null && options.incognito != newOptions.incognito)
setIncognito(newOptions.incognito);
if (newOptionsMap.get("hardwareAcceleration") != null && options.hardwareAcceleration != newOptions.hardwareAcceleration) {
if (newOptions.hardwareAcceleration)
setLayerType(View.LAYER_TYPE_HARDWARE, null);
else
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
if (newOptions.contentBlockers != null) { if (newOptions.contentBlockers != null) {
contentBlockerHandler.getRuleList().clear(); contentBlockerHandler.getRuleList().clear();
for (Map<String, Map<String, Object>> contentBlocker : newOptions.contentBlockers) { for (Map<String, Map<String, Object>> contentBlocker : newOptions.contentBlockers) {
......
...@@ -12,44 +12,46 @@ public class InAppWebViewOptions extends Options { ...@@ -12,44 +12,46 @@ public class InAppWebViewOptions extends Options {
public static final String LOG_TAG = "InAppWebViewOptions"; public static final String LOG_TAG = "InAppWebViewOptions";
public boolean useShouldOverrideUrlLoading = false; public Boolean useShouldOverrideUrlLoading = false;
public boolean useOnLoadResource = false; public Boolean useOnLoadResource = false;
public boolean useOnDownloadStart = false; public Boolean useOnDownloadStart = false;
public boolean useOnTargetBlank = false; public Boolean useOnTargetBlank = false;
public boolean clearCache = false; public Boolean clearCache = false;
public String userAgent = ""; public String userAgent = "";
public boolean javaScriptEnabled = true; public String applicationNameForUserAgent = "";
public boolean debuggingEnabled = false; public Boolean javaScriptEnabled = true;
public boolean javaScriptCanOpenWindowsAutomatically = false; public Boolean debuggingEnabled = false;
public boolean mediaPlaybackRequiresUserGesture = true; public Boolean javaScriptCanOpenWindowsAutomatically = false;
public Boolean mediaPlaybackRequiresUserGesture = true;
public Integer textZoom = 100; public Integer textZoom = 100;
public Integer minimumFontSize = 8; public Integer minimumFontSize = 8;
public boolean verticalScrollBarEnabled = true; public Boolean verticalScrollBarEnabled = true;
public boolean horizontalScrollBarEnabled = true; public Boolean horizontalScrollBarEnabled = true;
public List<String> resourceCustomSchemes = new ArrayList<>(); public List<String> resourceCustomSchemes = new ArrayList<>();
public List<Map<String, Map<String, Object>>> contentBlockers = new ArrayList<>(); public List<Map<String, Map<String, Object>>> contentBlockers = new ArrayList<>();
public Integer preferredContentMode = PreferredContentModeOptionType.RECOMMENDED.toValue(); public Integer preferredContentMode = PreferredContentModeOptionType.RECOMMENDED.toValue();
public Boolean useShouldInterceptAjaxRequest = false; public Boolean useShouldInterceptAjaxRequest = false;
public Boolean useShouldInterceptFetchRequest = false; public Boolean useShouldInterceptFetchRequest = false;
public Boolean incognito = false;
public Boolean cacheEnabled = true;
public Boolean transparentBackground = false;
public boolean clearSessionCache = false; public Boolean clearSessionCache = false;
public boolean builtInZoomControls = false; public Boolean builtInZoomControls = false;
public boolean displayZoomControls = false; public Boolean displayZoomControls = false;
public boolean supportZoom = true; public Boolean supportZoom = true;
public boolean databaseEnabled = false; public Boolean databaseEnabled = false;
public boolean domStorageEnabled = false; public Boolean domStorageEnabled = false;
public boolean useWideViewPort = true; public Boolean useWideViewPort = true;
public boolean safeBrowsingEnabled = true; public Boolean safeBrowsingEnabled = true;
public boolean transparentBackground = false;
public Integer mixedContentMode; public Integer mixedContentMode;
public boolean allowContentAccess = true; public Boolean allowContentAccess = true;
public boolean allowFileAccess = true; public Boolean allowFileAccess = true;
public boolean allowFileAccessFromFileURLs = true; public Boolean allowFileAccessFromFileURLs = true;
public boolean allowUniversalAccessFromFileURLs = true; public Boolean allowUniversalAccessFromFileURLs = true;
public boolean appCacheEnabled = true;
public String appCachePath; public String appCachePath;
public boolean blockNetworkImage = false; public Boolean blockNetworkImage = false;
public boolean blockNetworkLoads = false; public Boolean blockNetworkLoads = false;
public Integer cacheMode = WebSettings.LOAD_DEFAULT; public Integer cacheMode = WebSettings.LOAD_DEFAULT;
public String cursiveFontFamily = "cursive"; public String cursiveFontFamily = "cursive";
public Integer defaultFixedFontSize = 16; public Integer defaultFixedFontSize = 16;
...@@ -59,14 +61,17 @@ public class InAppWebViewOptions extends Options { ...@@ -59,14 +61,17 @@ public class InAppWebViewOptions extends Options {
public String fantasyFontFamily = "fantasy"; public String fantasyFontFamily = "fantasy";
public String fixedFontFamily = "monospace"; public String fixedFontFamily = "monospace";
public Integer forceDark = 0; // WebSettings.FORCE_DARK_OFF public Integer forceDark = 0; // WebSettings.FORCE_DARK_OFF
public boolean geolocationEnabled = true; public Boolean geolocationEnabled = true;
public WebSettings.LayoutAlgorithm layoutAlgorithm; public WebSettings.LayoutAlgorithm layoutAlgorithm;
public boolean loadWithOverviewMode = true; public Boolean loadWithOverviewMode = true;
public boolean loadsImagesAutomatically = true; public Boolean loadsImagesAutomatically = true;
public Integer minimumLogicalFontSize = 8; public Integer minimumLogicalFontSize = 8;
public boolean needInitialFocus = true; public Boolean needInitialFocus = true;
public boolean offscreenPreRaster = false; public Boolean offscreenPreRaster = false;
public String sansSerifFontFamily = "sans-serif"; public String sansSerifFontFamily = "sans-serif";
public String serifFontFamily = "sans-serif"; public String serifFontFamily = "sans-serif";
public String standardFontFamily = "sans-serif"; public String standardFontFamily = "sans-serif";
public Boolean saveFormData = true;
public Boolean thirdPartyCookiesEnabled = true;
public Boolean hardwareAcceleration = true;
} }
...@@ -93,7 +93,7 @@ class _InlineExampleScreenState extends State<InlineExampleScreen> { ...@@ -93,7 +93,7 @@ class _InlineExampleScreenState extends State<InlineExampleScreen> {
initialOptions: InAppWebViewWidgetOptions( initialOptions: InAppWebViewWidgetOptions(
inAppWebViewOptions: InAppWebViewOptions( inAppWebViewOptions: InAppWebViewOptions(
debuggingEnabled: true, debuggingEnabled: true,
clearCache: true, //clearCache: true,
useShouldOverrideUrlLoading: true, useShouldOverrideUrlLoading: true,
useOnTargetBlank: true, useOnTargetBlank: true,
useOnLoadResource: true, useOnLoadResource: true,
...@@ -113,7 +113,6 @@ class _InlineExampleScreenState extends State<InlineExampleScreen> { ...@@ -113,7 +113,6 @@ class _InlineExampleScreenState extends State<InlineExampleScreen> {
), ),
androidInAppWebViewOptions: AndroidInAppWebViewOptions( androidInAppWebViewOptions: AndroidInAppWebViewOptions(
databaseEnabled: true, databaseEnabled: true,
appCacheEnabled: true,
domStorageEnabled: true, domStorageEnabled: true,
geolocationEnabled: true, geolocationEnabled: true,
//safeBrowsingEnabled: true, //safeBrowsingEnabled: true,
......
...@@ -302,6 +302,10 @@ public class FlutterWebViewController: NSObject, FlutterPlatformView { ...@@ -302,6 +302,10 @@ public class FlutterWebViewController: NSObject, FlutterPlatformView {
} }
result(true) result(true)
break break
case "removeFromSuperview":
webView!.removeFromSuperview()
result(true)
break
default: default:
result(FlutterMethodNotImplemented) result(FlutterMethodNotImplemented)
break break
......
...@@ -26,6 +26,19 @@ func convertToDictionary(text: String) -> [String: Any]? { ...@@ -26,6 +26,19 @@ func convertToDictionary(text: String) -> [String: Any]? {
return nil return nil
} }
func JSONStringify(value: Any, prettyPrinted: Bool = false) -> String {
let options: JSONSerialization.WritingOptions = prettyPrinted ? .prettyPrinted : .init(rawValue: 0)
if JSONSerialization.isValidJSONObject(value) {
let data = try? JSONSerialization.data(withJSONObject: value, options: options)
if data != nil {
if let string = String(data: data!, encoding: .utf8) {
return string
}
}
}
return ""
}
let JAVASCRIPT_BRIDGE_NAME = "flutter_inappbrowser" let JAVASCRIPT_BRIDGE_NAME = "flutter_inappbrowser"
// the message needs to be concatenated with '' in order to have the same behavior like on Android // the message needs to be concatenated with '' in order to have the same behavior like on Android
...@@ -662,7 +675,6 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi ...@@ -662,7 +675,6 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi
let jscriptWebkitTouchCallout = WKUserScript(source: "document.body.style.webkitTouchCallout='none';", injectionTime: .atDocumentEnd, forMainFrameOnly: true) let jscriptWebkitTouchCallout = WKUserScript(source: "document.body.style.webkitTouchCallout='none';", injectionTime: .atDocumentEnd, forMainFrameOnly: true)
configuration.userContentController.addUserScript(jscriptWebkitTouchCallout) configuration.userContentController.addUserScript(jscriptWebkitTouchCallout)
let consoleLogJSScript = WKUserScript(source: consoleLogJS, injectionTime: .atDocumentStart, forMainFrameOnly: false) let consoleLogJSScript = WKUserScript(source: consoleLogJS, injectionTime: .atDocumentStart, forMainFrameOnly: false)
configuration.userContentController.addUserScript(consoleLogJSScript) configuration.userContentController.addUserScript(consoleLogJSScript)
configuration.userContentController.add(self, name: "consoleLog") configuration.userContentController.add(self, name: "consoleLog")
...@@ -699,18 +711,39 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi ...@@ -699,18 +711,39 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi
configuration.userContentController.addUserScript(interceptFetchRequestsJSScript) configuration.userContentController.addUserScript(interceptFetchRequestsJSScript)
} }
if #available(iOS 9.0, *) {
if ((options?.incognito)!) {
configuration.websiteDataStore = WKWebsiteDataStore.nonPersistent()
} else if ((options?.cacheEnabled)!) {
configuration.websiteDataStore = WKWebsiteDataStore.default()
}
}
//keyboardDisplayRequiresUserAction = browserOptions?.keyboardDisplayRequiresUserAction if #available(iOS 11.0, *) {
if((options?.sharedCookiesEnabled)!) {
// More info to sending cookies with WKWebView
// https://stackoverflow.com/questions/26573137/can-i-set-the-cookies-to-be-used-by-a-wkwebview/26577303#26577303
// Set Cookies in iOS 11 and above, initialize websiteDataStore before setting cookies
// See also https://forums.developer.apple.com/thread/97194
// check if websiteDataStore has not been initialized before
if(!(options?.incognito)! && !(options?.cacheEnabled)!) {
configuration.websiteDataStore = WKWebsiteDataStore.nonPersistent()
}
for cookie in HTTPCookieStorage.shared.cookies ?? [] {
configuration.websiteDataStore.httpCookieStore.setCookie(cookie, completionHandler: nil)
}
}
}
configuration.suppressesIncrementalRendering = (options?.suppressesIncrementalRendering)! configuration.suppressesIncrementalRendering = (options?.suppressesIncrementalRendering)!
allowsBackForwardNavigationGestures = (options?.allowsBackForwardNavigationGestures)! allowsBackForwardNavigationGestures = (options?.allowsBackForwardNavigationGestures)!
if #available(iOS 9.0, *) { if #available(iOS 9.0, *) {
allowsLinkPreview = (options?.allowsLinkPreview)! allowsLinkPreview = (options?.allowsLinkPreview)!
configuration.allowsPictureInPictureMediaPlayback = (options?.allowsPictureInPictureMediaPlayback)! configuration.allowsPictureInPictureMediaPlayback = (options?.allowsPictureInPictureMediaPlayback)!
if ((options?.applicationNameForUserAgent)! != "") { if (options?.applicationNameForUserAgent != nil && (options?.applicationNameForUserAgent)! != "") {
configuration.applicationNameForUserAgent = (options?.applicationNameForUserAgent)! configuration.applicationNameForUserAgent = (options?.applicationNameForUserAgent)!
} }
if ((options?.userAgent)! != "") { if (options?.userAgent != nil && (options?.userAgent)! != "") {
customUserAgent = (options?.userAgent)! customUserAgent = (options?.userAgent)!
} }
} }
...@@ -927,6 +960,25 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi ...@@ -927,6 +960,25 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi
} }
} }
if #available(iOS 9.0, *) {
if (newOptionsMap["incognito"] != nil && options?.incognito != newOptions.incognito && newOptions.incognito) {
configuration.websiteDataStore = WKWebsiteDataStore.nonPersistent()
} else if (newOptionsMap["cacheEnabled"] != nil && options?.cacheEnabled != newOptions.cacheEnabled && newOptions.cacheEnabled) {
configuration.websiteDataStore = WKWebsiteDataStore.default()
}
}
if #available(iOS 11.0, *) {
if (newOptionsMap["sharedCookiesEnabled"] != nil && options?.sharedCookiesEnabled != newOptions.sharedCookiesEnabled && newOptions.sharedCookiesEnabled) {
if(!newOptions.incognito && !newOptions.cacheEnabled) {
configuration.websiteDataStore = WKWebsiteDataStore.nonPersistent()
}
for cookie in HTTPCookieStorage.shared.cookies ?? [] {
configuration.websiteDataStore.httpCookieStore.setCookie(cookie, completionHandler: nil)
}
}
}
if newOptionsMap["enableViewportScale"] != nil && options?.enableViewportScale != newOptions.enableViewportScale && newOptions.enableViewportScale { if newOptionsMap["enableViewportScale"] != nil && options?.enableViewportScale != newOptions.enableViewportScale && newOptions.enableViewportScale {
let jscript = "var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);" let jscript = "var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);"
evaluateJavaScript(jscript, completionHandler: nil) evaluateJavaScript(jscript, completionHandler: nil)
...@@ -960,10 +1012,6 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi ...@@ -960,10 +1012,6 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi
configuration.allowsInlineMediaPlayback = newOptions.allowsInlineMediaPlayback configuration.allowsInlineMediaPlayback = newOptions.allowsInlineMediaPlayback
} }
// if newOptionsMap["keyboardDisplayRequiresUserAction"] != nil && browserOptions?.keyboardDisplayRequiresUserAction != newOptions.keyboardDisplayRequiresUserAction {
// self.webView.keyboardDisplayRequiresUserAction = newOptions.keyboardDisplayRequiresUserAction
// }
if newOptionsMap["suppressesIncrementalRendering"] != nil && options?.suppressesIncrementalRendering != newOptions.suppressesIncrementalRendering { if newOptionsMap["suppressesIncrementalRendering"] != nil && options?.suppressesIncrementalRendering != newOptions.suppressesIncrementalRendering {
configuration.suppressesIncrementalRendering = newOptions.suppressesIncrementalRendering configuration.suppressesIncrementalRendering = newOptions.suppressesIncrementalRendering
} }
...@@ -1997,4 +2045,24 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi ...@@ -1997,4 +2045,24 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi
func clearMatches(completionHandler: ((Any?, Error?) -> Void)?) { func clearMatches(completionHandler: ((Any?, Error?) -> Void)?) {
evaluateJavaScript("wkwebview_ClearMatches();", completionHandler: completionHandler) evaluateJavaScript("wkwebview_ClearMatches();", completionHandler: completionHandler)
} }
public override func removeFromSuperview() {
configuration.userContentController.removeScriptMessageHandler(forName: "consoleLog")
configuration.userContentController.removeScriptMessageHandler(forName: "consoleDebug")
configuration.userContentController.removeScriptMessageHandler(forName: "consoleError")
configuration.userContentController.removeScriptMessageHandler(forName: "consoleInfo")
configuration.userContentController.removeScriptMessageHandler(forName: "consoleWarn")
configuration.userContentController.removeScriptMessageHandler(forName: "callHandler")
configuration.userContentController.removeScriptMessageHandler(forName: "onFindResultReceived")
configuration.userContentController.removeScriptMessageHandler(forName: "onNavigationStateChange")
configuration.userContentController.removeAllUserScripts()
removeObserver(self, forKeyPath: "estimatedProgress")
super.removeFromSuperview()
uiDelegate = nil
navigationDelegate = nil
scrollView.delegate = nil
IAWController?.channel?.setMethodCallHandler(nil)
IABController?.webView = nil
IAWController?.webView = nil
}
} }
...@@ -17,6 +17,7 @@ public class InAppWebViewOptions: Options { ...@@ -17,6 +17,7 @@ public class InAppWebViewOptions: Options {
var useOnTargetBlank = false var useOnTargetBlank = false
var clearCache = false var clearCache = false
var userAgent = "" var userAgent = ""
var applicationNameForUserAgent = ""
var javaScriptEnabled = true var javaScriptEnabled = true
var debuggingEnabled = true var debuggingEnabled = true
var javaScriptCanOpenWindowsAutomatically = false var javaScriptCanOpenWindowsAutomatically = false
...@@ -25,13 +26,15 @@ public class InAppWebViewOptions: Options { ...@@ -25,13 +26,15 @@ public class InAppWebViewOptions: Options {
var horizontalScrollBarEnabled = true var horizontalScrollBarEnabled = true
var resourceCustomSchemes: [String] = [] var resourceCustomSchemes: [String] = []
var contentBlockers: [[String: [String : Any]]] = [] var contentBlockers: [[String: [String : Any]]] = []
var minimumFontSize = 0; var minimumFontSize = 0
var useShouldInterceptAjaxRequest = false var useShouldInterceptAjaxRequest = false
var useShouldInterceptFetchRequest = false var useShouldInterceptFetchRequest = false
var incognito = false
var cacheEnabled = true
var transparentBackground = false
var disallowOverScroll = false var disallowOverScroll = false
var enableViewportScale = false var enableViewportScale = false
//var keyboardDisplayRequiresUserAction = true
var suppressesIncrementalRendering = false var suppressesIncrementalRendering = false
var allowsAirPlayForMediaPlayback = true var allowsAirPlayForMediaPlayback = true
var allowsBackForwardNavigationGestures = true var allowsBackForwardNavigationGestures = true
...@@ -39,12 +42,11 @@ public class InAppWebViewOptions: Options { ...@@ -39,12 +42,11 @@ public class InAppWebViewOptions: Options {
var ignoresViewportScaleLimits = false var ignoresViewportScaleLimits = false
var allowsInlineMediaPlayback = false var allowsInlineMediaPlayback = false
var allowsPictureInPictureMediaPlayback = true var allowsPictureInPictureMediaPlayback = true
var transparentBackground = false
var applicationNameForUserAgent = "";
var isFraudulentWebsiteWarningEnabled = true; var isFraudulentWebsiteWarningEnabled = true;
var selectionGranularity = 0; var selectionGranularity = 0;
var dataDetectorTypes: [String] = ["NONE"]; // WKDataDetectorTypeNone var dataDetectorTypes: [String] = ["NONE"] // WKDataDetectorTypeNone
var preferredContentMode = 0; var preferredContentMode = 0
var sharedCookiesEnabled = false
override init(){ override init(){
super.init() super.init()
......
...@@ -325,6 +325,13 @@ class _InAppWebViewState extends State<InAppWebView> { ...@@ -325,6 +325,13 @@ class _InAppWebViewState extends State<InAppWebView> {
super.didUpdateWidget(oldWidget); super.didUpdateWidget(oldWidget);
} }
@override
void dispose(){
super.dispose();
if (Platform.isIOS)
_controller._channel.invokeMethod('removeFromSuperview');
}
void _onPlatformViewCreated(int id) { void _onPlatformViewCreated(int id) {
_controller = InAppWebViewController(id, widget); _controller = InAppWebViewController(id, widget);
if (widget.onWebViewCreated != null) { if (widget.onWebViewCreated != null) {
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment