How To Clear WebView Cookies In Xamarin.Forms Using DependencyService

Introduction 

Sometimes, we may need to clear the WebView cookies in our app. For example, in social media integration, we need to clear the cookies for user logout. In this article, we can learn how to clear cookies using DependencyService concept in Xamarin.Forms.

Requirements

  • This article's source code is prepared by using Visual Studio. And it is better to install the latest Visual Studio updates from here.
  • This article is prepared on a MAC machine.
  • This sample project is Xamarin.Forms PCL project.
  • This sample app is targeted for Android, iOS. And, tested for Android & iOS.

Description

First, follow the below steps to create the new Xamarin.Forms project.

Open Visual Studio for Mac.

  • Click on the File menu and select New Solution.
  • In the left pane of the dialog, let's select the type of templates to display. Multiplatform > App > Xamarin.Forms > Blank Forms App and click "Next".
  • Next, enter your app name (Ex: WebViewCookiesDemo). At the bottom, select target platforms to Android & iOS and shared code to Portable Class Library and click the "Next" button.
  • Then, choose project location with the help of Browse button and click "Create".

Now, the project structure will be created like below.

  • WebViewCookiesDemo is for shared code.
  • WebViewCookiesDemo.Droid is for Android.
  • WebViewCookiesDemo.iOS  is for iOS.

1) Xamarin.Forms

Portable Class Library(PCL)

Create an interface IClearCookies inside the DependencyServices folder and having method declaration of ClearAllCookies.

IClearCookies.cs

  1. namespace WebViewCookiesDemo.DependencyServices {  
  2.     public interface IClearCookies {  
  3.         void ClearAllCookies();  
  4.     }  
  5. }  

Xamarin.Android

Create a class ClearCookies and need to implement IClearCookies method like below.

ClearCookies.cs

  1. using WebViewCookiesDemo.Droid;  
  2. using Xamarin.Forms;  
  3. using Android.Webkit;  
  4. using WebViewCookiesDemo.DependencyServices;  
  5. [assembly: Dependency(typeof(ClearCookies))]  
  6. namespace WebViewCookiesDemo.Droid {  
  7.     public class ClearCookies: IClearCookies {  
  8.         public void ClearAllCookies() {  
  9.             var cookieManager = CookieManager.Instance;  
  10.             cookieManager.RemoveAllCookie();  
  11.         }  
  12.     }  
  13. }  

Note
CookieManager provides a concrete implementation of CookieHandler, which separates the storage of cookies from the policy surrounding accepting and rejecting cookies. A CookieManager is initialised with a CookieStore which manages storage, and a CookiePolicy object, which makes policy decisions on cookie acceptance/rejection.

Xamarin.iOS

In iOS also, create a class ClearCookies and need to implement IClearCookies method like below.

ClearCookies.cs

  1. using WebViewCookiesDemo.iOS;  
  2. using Xamarin.Forms;  
  3. using Foundation;  
  4. using WebViewCookiesDemo.DependencyServices;  
  5. [assembly: Dependency(typeof(ClearCookies))]  
  6. namespace WebViewCookiesDemo.iOS {  
  7.     public class ClearCookies: IClearCookies {  
  8.         public void ClearAllCookies() {  
  9.             NSHttpCookieStorage CookieStorage = NSHttpCookieStorage.SharedStorage;  
  10.             foreach(var cookie in CookieStorage.Cookies)  
  11.             CookieStorage.DeleteCookie(cookie);  
  12.         }  
  13.     }  
  14. }  

Note
NSHttpCookieStorage is a singleton object (shared instance) that manages storage of cookies in iOS.

Call to DependencyService

Now, we are calling the dependency service to clear cookies.

  1. DependencyService.Get<IClearCookies>().ClearAllCookies();  

We need to call the above dependency service in the page where we want to clear cookies.

2) Xamarin.Andorid

  1. var cookieManager = CookieManager.Instance;  
  2. cookieManager.RemoveAllCookie();  

3) Xamarin.iOS

  1. NSHttpCookieStorage CookieStorage = NSHttpCookieStorage.SharedStorage;  
  2. foreach (var cookie in CookieStorage.Cookies)   
  3. CookieStorage.DeleteCookie(cookie);   
Please download source code from here.