Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to Clear Cookies in Flutter Custom Tabs? #227

Closed
GowthamanRavichandran3 opened this issue Jan 6, 2025 · 5 comments
Closed

How to Clear Cookies in Flutter Custom Tabs? #227

GowthamanRavichandran3 opened this issue Jan 6, 2025 · 5 comments

Comments

@GowthamanRavichandran3
Copy link

I am using the flutter_custom_tabs package to open a URL in my Flutter app. I need to clear the cookies stored in the browser when the custom tabs browser is closed.

I used a native method (Kotlin for Android, swift for iOS) to clear cookies, but it didn’t work as expected. Although the native scripts executed correctly when called from Flutter, the cookies in the custom tabs browser remained uncleared.

Below is the native code I used:

Android:

privatefun clearBrowserCache() {
        WebView(this).clearCache(true)
        CookieManager.getInstance().removeAllCookies(null)
        WebStorage.getInstance().deleteAllData()
    }

iOS:

privatefuncclearBrowserCache() {
let dataStore =WKWebsiteDataStore.default()
let dataTypes =WKWebsiteDataStore.allWebsiteDataTypes()
let dateFrom =Date(timeIntervalSince1970: 0)
        dataStore.removeData(ofTypes: dataTypes, modifiedSince: dateFrom, completionHandler: {})
    }

Is there a way to clear the cookies of the browser opened with flutter_custom_tabs? Or is there any workaround or alternative solution to achieve this functionality?

@droibit
Copy link
Owner

droibit commented Jan 7, 2025

Hi,

Your current approach using WebView methods won't clear cookies in the custom tabs browser,
as flutter_custom_tabs internally uses Custom Tabs (Android) and SFSafariViewController (iOS).

Unfortunately, clearing cookies for Custom Tabs is platform-specific and has limitations:

Consider using webview_flutter for more granular control over web views and cookie management.

@GowthamanRavichandran3
Copy link
Author

Hi @droibit,

Thanks for your response. I tried webview_flutter and flutter_inappwebview, but neither of them showed the already-signed-in account when I tried Google sign-in. Every time you try to log in, it will ask for your password and email address. I tried using flutter_custom_tabs for that. Are there any other options for showing Google accounts when using flutter_inappwebview or webview_flutter?

@droibit
Copy link
Owner

droibit commented Jan 8, 2025

@GowthamanRavichandran3
I'm not very familiar with the webview_flutter and flutter_inappwebview packages.

However, when implementing Google sign-in in Flutter, you might consider using authentication packages like these:

@GowthamanRavichandran3
Copy link
Author

Hi @droibit, I am using CustomTabsIntent from the native method instead of flutter_custom_tabs to launch a URL in my Android app. Is there a way to clear cookies when using CustomTabsIntent? I want to ensure the browser session starts fresh without retaining any previous cookies or login information. How can I achieve this?

Below is the code snippet I used:

val customTabsIntent = CustomTabsIntent.Builder().build() customTabsIntent.launchUrl(this, Uri.parse(loginUrl))

@droibit
Copy link
Owner

droibit commented Jan 23, 2025

Unfortunately, CustomTabsIntent doesn't offer a built-in method to clear cookies.
Due to this, I have no plans to add cookie clearing functionality to flutter_custom_tabs.

For more specific guidance on managing cookies in your use case,
consider posting your question on a platform like Stack Overflow.
(If you're using OpenID Connect, you might be able to force re-authentication by adding the prompt=login parameter to your authentication URL.)

I will close this issue for now.

@droibit droibit closed this as completed Jan 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants