This is an automated email from the ASF dual-hosted git repository. GitToTheHub pushed a commit to branch pr-ios-refactor-clear-website-data-code in repository https://gitbox.apache.org/repos/asf/cordova-plugin-inappbrowser.git
commit cf9f535e0035cf2742432460cf91409d4ccae0ea Author: Manuel Beck <[email protected]> AuthorDate: Fri Apr 24 10:37:17 2026 +0200 refactor(ios): refactor code for clearing data - Create method `clearWebsiteDataByOptions` which contains the code for the three options `cleardata`, `clearcache` and `clearsessioncache` - Make clear that using these options will also clear the data of the Cordova WebView. Improve Readme to reflect this. - On Android these three options will also clear the data of the Cordova WebView, which is also added to the Readme. --- README.md | 10 +++--- src/ios/CDVWKInAppBrowser.m | 84 ++++++++++++++++++++++++--------------------- 2 files changed, 49 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index 3abc4ae..bb2fd3d 100644 --- a/README.md +++ b/README.md @@ -105,8 +105,8 @@ instance, or the system browser. - __hidden__: set to `yes` to create the browser and load the page, but not show it. The loadstop event fires when loading is complete. Omit or set to `no` (default) to have the browser open and load normally. - __beforeload__: set to enable the `beforeload` event to modify which pages are actually loaded in the browser. Accepted values are `get` to intercept only GET requests, `post` to intercept on POST requests or `yes` to intercept both GET & POST requests. Note that POST requests are not currently supported and will be ignored (if you set `beforeload=post` it will raise an error). - - __clearcache__: set to `yes` to have the browser's cookie cache cleared before the new window is opened - - __clearsessioncache__: set to `yes` to have the session cookie cache cleared before the new window is opened + - __clearcache__: Set to `yes` to clear the shared browser's cookie cache of your app, which means also the one of the Cordova WebView, before the in-app browser is opened. + - __clearsessioncache__: Set to `yes` to clear the shared session cookies from the browser's cache of your app, which means also the one of the Cordova WebView, before the in-app browser is opened. - __closebuttoncaption__: set to a string to use as the close button's caption instead of a X. Note that you need to localize this value yourself. - __closebuttoncolor__: set to a valid hex color string, for example: `#00ff00`, and it will change the close button color from default, regardless of being a text or default X. Only has effect if user has location set to `yes`. @@ -130,9 +130,9 @@ instance, or the system browser. - __hidden__: set to `yes` to create the browser and load the page, but not show it. The loadstop event fires when loading is complete. Omit or set to `no` (default) to have the browser open and load normally. - __beforeload__: set to enable the `beforeload` event to modify which pages are actually loaded in the browser. Accepted values are `get` to intercept only GET requests, `post` to intercept on POST requests or `yes` to intercept both GET & POST requests. Note that POST requests are not currently supported and will be ignored (if you set `beforeload=post` it will raise an error). - - __clearcache__: set to `yes` to have the browser's cookie cache cleared before the new window is opened - - __clearsessioncache__: set to `yes` to have the session cookie cache cleared before the new window is opened. For WKWebView, requires iOS 11+ on target device. - - __cleardata__: set to `yes` to have the browser's entire local storage cleared (cookies, HTML5 local storage, IndexedDB, etc.) before the new window is opened + - __clearcache__: Set to `yes` to clear the browser's cookie cache of your app, which means also the one of the Cordova WebView, before the in-app browser is opened. + - __clearsessioncache__: Set to `yes` to clear the shared session cookies from the browser's cache of your app, which means also the one of the Cordova WebView, before the in-app browser is opened. + - __cleardata__: Set to `yes` to clear the shared browser's entire local storage of your app, which means also the one of the Cordova WebView, like cookies, HTML5 local storage, IndexedDB, etc., before the in-app browser is opened. - __closebuttoncolor__: set as a valid hex color string, for example: `#00ff00`, to change from the default __Done__ button's color. Only applicable if toolbar is not disabled. - __closebuttoncaption__: set to a string to use as the __Done__ button's caption. Note that you need to localize this value yourself. - __disallowoverscroll__: Set to `yes` or `no` (default is `no`). Turns on/off the the bounce of the WKWebView's UIScrollView. diff --git a/src/ios/CDVWKInAppBrowser.m b/src/ios/CDVWKInAppBrowser.m index 71c1c9a..93622b6 100644 --- a/src/ios/CDVWKInAppBrowser.m +++ b/src/ios/CDVWKInAppBrowser.m @@ -111,47 +111,8 @@ - (void)openInInAppBrowser:(NSURL *)url withOptions:(NSString *)options { CDVInAppBrowserOptions *browserOptions = [CDVInAppBrowserOptions parseOptions:options]; - WKWebsiteDataStore *dataStore = [WKWebsiteDataStore defaultDataStore]; - - if (browserOptions.cleardata) { - NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0]; - [dataStore removeDataOfTypes:[WKWebsiteDataStore allWebsiteDataTypes] modifiedSince:dateFrom completionHandler:^{ - NSLog(@"Removed all WKWebView data"); - if (@available(iOS 15.0, *)) { - // Since iOS 15 WKProcessPool is deprecated and has no effect - } else { -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdeprecated-declarations" - // Set for iOS 14 and below a new process pool - self.inAppBrowserViewController.webView.configuration.processPool = [[WKProcessPool alloc] init]; // create new process pool to flush all data -#pragma clang diagnostic pop - } - }]; - } - if (browserOptions.clearcache) { - // Deletes all cookies - WKHTTPCookieStore *cookieStore = dataStore.httpCookieStore; - [cookieStore getAllCookies:^(NSArray *cookies) { - NSHTTPCookie *cookie; - for (cookie in cookies) { - [cookieStore deleteCookie:cookie completionHandler:nil]; - } - }]; - } - - if (browserOptions.clearsessioncache) { - // Deletes session cookies - WKHTTPCookieStore *cookieStore = dataStore.httpCookieStore; - [cookieStore getAllCookies:^(NSArray *cookies) { - NSHTTPCookie *cookie; - for (cookie in cookies) { - if (cookie.sessionOnly) { - [cookieStore deleteCookie:cookie completionHandler:nil]; - } - } - }]; - } + [self clearWebsiteDataByOptions:browserOptions]; if (self.inAppBrowserViewController == nil) { self.inAppBrowserViewController = [[CDVWKInAppBrowserViewController alloc] initWithBrowserOptions: browserOptions andSettings:self.commandDelegate.settings]; @@ -213,6 +174,49 @@ } } +/** + * Clears website data (cookies, cache, etc.) according to the specified options. + * This will clear the data on the Cordova WebView also. + * + * @param browserOptions The options specifying which types of data to clear. + */ +- (void)clearWebsiteDataByOptions:(CDVInAppBrowserOptions *)browserOptions +{ + // NOTE: [WKWebsiteDataStore defaultDataStore] will get the default data store of the app, + // which is shared between all WKWebViews, which means also the Cordova WebView + WKWebsiteDataStore *dataStore = [WKWebsiteDataStore defaultDataStore]; + + if (browserOptions.cleardata) { + [dataStore fetchDataRecordsOfTypes:WKWebsiteDataStore.allWebsiteDataTypes completionHandler:^(NSArray<WKWebsiteDataRecord *> * _Nonnull records) { + [dataStore removeDataOfTypes:WKWebsiteDataStore.allWebsiteDataTypes forDataRecords:records completionHandler:^{ + NSLog(@"Removed all WKWebView data"); + // Create new WKProcessPool + if (@available(iOS 15.0, *)) { + // Since iOS 15 WKProcessPool is deprecated and has no effect + } else { +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" + // Set for iOS 14 and below a new process pool + self.inAppBrowserViewController.webView.configuration.processPool = [[WKProcessPool alloc] init]; // create new process pool to flush all data +#pragma clang diagnostic pop + } + }]; + }]; + } + + // Deletes all cookies or session cookies + if (browserOptions.clearcache || browserOptions.clearsessioncache) { + WKHTTPCookieStore *cookieStore = dataStore.httpCookieStore; + [cookieStore getAllCookies:^(NSArray *cookies) { + for (NSHTTPCookie *cookie in cookies) { + if (browserOptions.clearcache || (browserOptions.clearsessioncache && cookie.sessionOnly)) { + [cookieStore deleteCookie:cookie completionHandler:nil]; + } + } + }]; + } +} + - (void)show:(CDVInvokedUrlCommand *)command { [self show:command withNoAnimate:NO]; --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
