Wednesday, 16 May 2018

How to clear WebView Cookies in Xamarin.Forms

Introduction:

Sometimes we may needs to clear the WebView cookies in our app, for example integrated social media login, in that case we need to clear the cookies, In this article we can learn how to clear cookies using DependencyServices 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 on 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 on Next button.
  • Then choose project location with the help of Browse button and click on create.
Now project structure will be created like below.

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

1) Xamarin.Forms

Portable Class Library(PCL):

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

IClipboardService.cs

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

Xamarin.Android:
Create a class ClearCookies and need to implement ClearCookies method like below.

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

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 CopyToClipboard method like below.

ClearCookies.cs

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

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

Call to DependencyService:
Now calling dependency service to clear cookies.


  1. DependencyService.Get<IClearCookies>().ClearAllCookies();    
We need to call 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);    

Source Code:

No comments:

Post a Comment