How do I access cookies in a Xamarin WebView?

Simon picture Simon · Aug 11, 2016 · Viewed 10.1k times · Source

In our Xamarin app, we need to allow our users to log in using their own SSO solution.

The way this works in our iOS app is that we open up a controller with a web view and point it at our site, and keep watching the cookies until they're back on our site with the cookie that indicates that they're logged in. But in Xamarin, I can't see how I can access the cookies for the WebView. Our Xamarin app is running on Windows 8.1 and WinPhone 8.1, but it will be extended to Android at some point so I need a cross-platform solution.

So, how do I access cookies in a Xamarin WebView?

Answer

Joehl picture Joehl · Aug 11, 2016

You need to create a custom control in your PCL-Project and then add a custom webview for each platform. The platform secific implementation then gets the coockies and you can use it from your pcl-webview.

On Android you can get the cookies with following code:

var cookieHeader = CookieManager.Instance.GetCookie(url);

And on iOS:

NSHttpCookieStorage storage = NSHttpCookieStorage.SharedStorage;

Sourcecode on github: https://github.com/seansparkman/CookiesWebView