//
// UniWebView.Cookie.cs
// Created by Wang Wei (@onevcat) on 2025-04-19.
//
// This file is a part of UniWebView Project (https://uniwebview.com)
// By purchasing the asset, you are allowed to use this code in as many as projects
// you want, only if you publish the final products under the name of the same account
// used for the purchase.
//
// This asset and all corresponding files (such as source code) are provided on an
// “as is” basis, without warranty of any kind, express of implied, including but not
// limited to the warranties of merchantability, fitness for a particular purpose, and
// noninfringement. In no event shall the authors or copyright holders be liable for any
// claim, damages or other liability, whether in action of contract, tort or otherwise,
// arising from, out of or in connection with the software or the use of other dealing in the software.
//
using System;
using System.Threading.Tasks;
public partial class UniWebView {
///
/// Sets whether the UniWebView should allow third party cookies to be set. By default, on Android, the third party
/// cookies are disallowed due to security reason. Setting this to `true` will allow the cookie manager to accept
/// third party cookies you set.
///
/// This method only works for Android. On iOS, this method does nothing because WKWebView fundamentally does not
/// support third party cookies. This is a deliberate architectural limitation of WKWebView for privacy and security
/// reasons. Third party cookies are always blocked on iOS and cannot be enabled through any API. If your application
/// requires third party cookie support, consider using alternative authentication methods such as OAuth or JWT tokens.
///
/// Note: Starting from iOS 18, the default SameSite cookie attribute changed from "None" to "Lax", which may affect
/// cross-site cookie behavior even for first party cookies.
///
/// Whether the third party cookies should be allowed.
public void SetAcceptThirdPartyCookies(bool flag) {
#if UNITY_ANDROID && !UNITY_EDITOR
UniWebViewInterface.SetAcceptThirdPartyCookies(listener.Name, flag);
#endif
}
///
/// Clears all cookies from web view. When it finishes, the `handler` will be called.
///
/// This will clear cookies from all domains in the web view and previous.
/// If you only need to remove cookies from a certain domain, use `RemoveCookies` instead.
///
/// An action that is called after the operation finishes.
public static void ClearCookies(Action handler) {
var identifier = Guid.NewGuid().ToString();
globalPayloadActions.Add(identifier, (payload) => {
handler.Invoke();
});
UniWebViewInterface.ClearCookies(identifier);
}
///
/// Clears all cookies from web view asynchronously.
///
/// This will clear cookies from all domains in the web view and previous.
/// If you only need to remove cookies from a certain domain, use `RemoveCookies` instead.
///
public static async Task ClearCookiesAsync() {
var tcs = new TaskCompletionSource