UniWebView.Cookie.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. //
  2. // UniWebView.Cookie.cs
  3. // Created by Wang Wei (@onevcat) on 2025-04-19.
  4. //
  5. // This file is a part of UniWebView Project (https://uniwebview.com)
  6. // By purchasing the asset, you are allowed to use this code in as many as projects
  7. // you want, only if you publish the final products under the name of the same account
  8. // used for the purchase.
  9. //
  10. // This asset and all corresponding files (such as source code) are provided on an
  11. // “as is” basis, without warranty of any kind, express of implied, including but not
  12. // limited to the warranties of merchantability, fitness for a particular purpose, and
  13. // noninfringement. In no event shall the authors or copyright holders be liable for any
  14. // claim, damages or other liability, whether in action of contract, tort or otherwise,
  15. // arising from, out of or in connection with the software or the use of other dealing in the software.
  16. //
  17. using System;
  18. using System.Threading.Tasks;
  19. public partial class UniWebView {
  20. /// <summary>
  21. /// Sets whether the UniWebView should allow third party cookies to be set. By default, on Android, the third party
  22. /// cookies are disallowed due to security reason. Setting this to `true` will allow the cookie manager to accept
  23. /// third party cookies you set.
  24. ///
  25. /// This method only works for Android. On iOS, this method does nothing because WKWebView fundamentally does not
  26. /// support third party cookies. This is a deliberate architectural limitation of WKWebView for privacy and security
  27. /// reasons. Third party cookies are always blocked on iOS and cannot be enabled through any API. If your application
  28. /// requires third party cookie support, consider using alternative authentication methods such as OAuth or JWT tokens.
  29. ///
  30. /// Note: Starting from iOS 18, the default SameSite cookie attribute changed from "None" to "Lax", which may affect
  31. /// cross-site cookie behavior even for first party cookies.
  32. /// </summary>
  33. /// <param name="flag">Whether the third party cookies should be allowed.</param>
  34. public void SetAcceptThirdPartyCookies(bool flag) {
  35. #if UNITY_ANDROID && !UNITY_EDITOR
  36. UniWebViewInterface.SetAcceptThirdPartyCookies(listener.Name, flag);
  37. #endif
  38. }
  39. /// <summary>
  40. /// Clears all cookies from web view. When it finishes, the `handler` will be called.
  41. ///
  42. /// This will clear cookies from all domains in the web view and previous.
  43. /// If you only need to remove cookies from a certain domain, use `RemoveCookies` instead.
  44. /// </summary>
  45. /// <param name="handler">An action that is called after the operation finishes.</param>
  46. public static void ClearCookies(Action handler) {
  47. var identifier = Guid.NewGuid().ToString();
  48. globalPayloadActions.Add(identifier, (payload) => {
  49. handler.Invoke();
  50. });
  51. UniWebViewInterface.ClearCookies(identifier);
  52. }
  53. /// <summary>
  54. /// Clears all cookies from web view asynchronously.
  55. ///
  56. /// This will clear cookies from all domains in the web view and previous.
  57. /// If you only need to remove cookies from a certain domain, use `RemoveCookies` instead.
  58. /// </summary>
  59. public static async Task ClearCookiesAsync() {
  60. var tcs = new TaskCompletionSource<object>();
  61. ClearCookies(() => {
  62. tcs.SetResult(null);
  63. });
  64. await tcs.Task;
  65. }
  66. /// <summary>
  67. /// Sets a cookie for a certain url. When it finishes, the `handler` will be called.
  68. /// </summary>
  69. /// <param name="url">The url to which cookie will be set.</param>
  70. /// <param name="cookie">The cookie string to set.</param>
  71. /// <param name="handler">An action that is called after the operation finishes.</param>
  72. public static void SetCookie(string url, string cookie, Action handler) {
  73. SetCookie(url, cookie, false, handler);
  74. }
  75. /// <summary>
  76. /// Sets a cookie for a certain url. When it finishes, the `handler` will be called.
  77. /// </summary>
  78. /// <param name="url">The url to which cookie will be set.</param>
  79. /// <param name="cookie">The cookie string to set.</param>
  80. /// <param name="skipEncoding">
  81. /// Whether UniWebView should skip encoding the url or not. If set to `false`, UniWebView will try to encode the url parameter before
  82. /// using it. Otherwise, your original url string will be used to set the cookie if it is valid.
  83. /// </param>
  84. /// <param name="handler">An action that is called after the operation finishes.</param>
  85. public static void SetCookie(string url, string cookie, bool skipEncoding, Action handler) {
  86. var identifier = Guid.NewGuid().ToString();
  87. globalPayloadActions.Add(identifier, (payload) => {
  88. handler.Invoke();
  89. });
  90. UniWebViewInterface.SetCookie(url, cookie, skipEncoding, identifier);
  91. }
  92. /// <summary>
  93. /// Sets a cookie for a certain url asynchronously.
  94. /// </summary>
  95. /// <param name="url">The url to which cookie will be set.</param>
  96. /// <param name="cookie">The cookie string to set.</param>
  97. /// <param name="skipEncoding">
  98. /// Whether UniWebView should skip encoding the url or not. If set to `false`, UniWebView will try to encode the url parameter before
  99. /// using it. Otherwise, your original url string will be used to set the cookie if it is valid. Default is `false`.
  100. /// </param>
  101. public static async Task SetCookieAsync(string url, string cookie, bool skipEncoding = false) {
  102. var tcs = new TaskCompletionSource<object>();
  103. SetCookie(url, cookie, skipEncoding, () => {
  104. tcs.SetResult(null);
  105. });
  106. await tcs.Task;
  107. }
  108. /// <summary>
  109. /// Gets the cookie value under a given url and key. When it finishes, the `handler` will be called with the cookie value
  110. /// if exists.
  111. /// </summary>
  112. /// <param name="url">The url (domain) where the target cookie is.</param>
  113. /// <param name="key">The key for target cookie value.</param>
  114. /// <param name="skipEncoding">
  115. /// Whether UniWebView should skip encoding the url or not. If set to `false`, UniWebView will try to encode the url parameter before
  116. /// using it. Otherwise, your original url string will be used to get the cookie if it is valid.
  117. /// </param>
  118. /// <param name="handler">An action that is called after the operation finishes.</param>
  119. public static void GetCookie(string url, string key, bool skipEncoding, Action<string> handler) {
  120. var identifier = Guid.NewGuid().ToString();
  121. globalPayloadActions.Add(identifier, (payload) => {
  122. handler.Invoke(payload.data);
  123. });
  124. UniWebViewInterface.GetCookie(url, key, skipEncoding, identifier);
  125. }
  126. /// <summary>
  127. /// Gets the cookie value under a given url and key asynchronously.
  128. ///
  129. /// </summary>
  130. /// <param name="url">The url (domain) where the target cookie is.</param>
  131. /// <param name="key">The key for target cookie value.</param>
  132. /// <param name="skipEncoding">
  133. /// Whether UniWebView should skip encoding the url or not. If set to `false`, UniWebView will try to encode the url parameter before
  134. /// using it. Otherwise, your original url string will be used to get the cookie if it is valid. Default is `false`.
  135. /// </param>
  136. public static async Task<string> GetCookieAsync(string url, string cookieName, bool skipEncoding = false) {
  137. var tcs = new TaskCompletionSource<string>();
  138. GetCookie(url, cookieName, skipEncoding, (result) => {
  139. tcs.SetResult(result);
  140. });
  141. return await tcs.Task;
  142. }
  143. /// <summary>
  144. /// Removes all the cookies under a given url. When it finishes, the `handler` will be called.
  145. /// </summary>
  146. /// <param name="url">The url (domain) where the cookies are under.</param>
  147. /// <param name="handler">An action that is called after the operation finishes.</param>
  148. public static void RemoveCookies(string url, Action handler) {
  149. RemoveCookies(url, false, handler);
  150. }
  151. /// <summary>
  152. /// Removes all the cookies under a given url. When it finishes, the `handler` will be called.
  153. /// </summary>
  154. /// <param name="url">The url (domain) where the cookies are under.</param>
  155. /// <param name="handler">An action that is called after the operation finishes.</param>
  156. /// <param name="skipEncoding">
  157. /// Whether UniWebView should skip encoding the url or not. If set to `false`, UniWebView will try to encode the url parameter before
  158. /// using it. Otherwise, your original url string will be used to get the cookie if it is valid.
  159. /// </param>
  160. public static void RemoveCookies(string url, bool skipEncoding, Action handler) {
  161. var identifier = Guid.NewGuid().ToString();
  162. globalPayloadActions.Add(identifier, (payload) => {
  163. handler.Invoke();
  164. });
  165. UniWebViewInterface.RemoveCookies(url, skipEncoding, identifier);
  166. }
  167. /// <summary>
  168. /// Removes all the cookies under a given url asynchronously.
  169. /// </summary>
  170. /// <param name="url">The url (domain) where the cookies are under.</param>
  171. /// <param name="handler">An action that is called after the operation finishes.</param>
  172. /// <param name="skipEncoding">
  173. /// Whether UniWebView should skip encoding the url or not. If set to `false`, UniWebView will try to encode the url parameter before
  174. /// using it. Otherwise, your original url string will be used to get the cookie if it is valid. Default is `false`.
  175. /// </param>
  176. public static async Task RemoveCookiesAsync(string url, bool skipEncoding = false) {
  177. var tcs = new TaskCompletionSource<object>();
  178. RemoveCookies(url, skipEncoding, () => {
  179. tcs.SetResult(null);
  180. });
  181. await tcs.Task;
  182. }
  183. /// <summary>
  184. /// Removes a certain cookie under the given url for the specified key. When it finishes, the `handler` will be called.
  185. /// </summary>
  186. /// <param name="url">The url (domain) where the cookies are under.</param>
  187. /// <param name="key">The key for target cookie.</param>
  188. /// <param name="handler">An action that is called after the operation finishes.</param>
  189. public static void RemoveCookie(string url, string key, Action handler) {
  190. RemoveCookie(url, key, false, handler);
  191. }
  192. /// <summary>
  193. /// Removes a certain cookie under the given url for the specified key. When it finishes, the `handler` will be called.
  194. /// </summary>
  195. /// <param name="url">The url (domain) where the cookies are under.</param>
  196. /// <param name="key">The key for target cookie.</param>
  197. /// <param name="skipEncoding">
  198. /// Whether UniWebView should skip encoding the url or not. If set to `false`, UniWebView will try to encode the url parameter before
  199. /// using it. Otherwise, your original url string will be used to get the cookie if it is valid.
  200. /// </param>
  201. /// <param name="handler">An action that is called after the operation finishes.</param>
  202. public static void RemoveCookie(string url, string key, bool skipEncoding, Action handler) {
  203. var identifier = Guid.NewGuid().ToString();
  204. globalPayloadActions.Add(identifier, (payload) => {
  205. handler.Invoke();
  206. });
  207. UniWebViewInterface.RemoveCookie(url, key, skipEncoding, identifier);
  208. }
  209. /// <summary>
  210. /// Removes a certain cookie under the given url for the specified key. asynchronously
  211. /// </summary>
  212. /// <param name="url">The url (domain) where the cookies are under.</param>
  213. /// <param name="key">The key for target cookie.</param>
  214. /// <param name="skipEncoding">
  215. /// Whether UniWebView should skip encoding the url or not. If set to `false`, UniWebView will try to encode the url parameter before
  216. /// using it. Otherwise, your original url string will be used to get the cookie if it is valid. Default is `false`.
  217. /// </param>
  218. public static async Task RemoveCookieAsync(string url, string key, bool skipEncoding = false) {
  219. var tcs = new TaskCompletionSource<object>();
  220. RemoveCookie(url, key, skipEncoding, () => {
  221. tcs.SetResult(null);
  222. });
  223. await tcs.Task;
  224. }
  225. }