Cookies in xamarin iOS web browser

I want to set cookies on a web view. Is there any help in this regard?

NSUrl urlq = new NSUrl (url);
webview = new UIWebView ();

webview.LoadRequest(new NSUrlRequest(urlq));
webview.Frame = new RectangleF (0,0, webViewForLoad.Frame.Width, webViewForLoad.Frame.Height);
webview.AllowsInlineMediaPlayback = true;
//webview.LoadRequest (new NSUrl (url, false));
webview.ScalesPageToFit = true;
webViewForLoad.AddSubview (webview);

      

+3


source to share


1 answer


You need to set a cookie on the shared storage. First, set a shared storage policy to always accept your own cookies. This can be put in your ApplicationDelegate (say ApplicationDidBecomeActive).

NSHttpCookieStorage.SharedStorage.AcceptPolicy = NSHttpCookieAcceptPolicy.Always;

      

Create your cookie and set it on shared storage.

var cookieDict = new NSMutableDictionary ();
cookieDict.Add (NSHttpCookie.KeyOriginURL, new NSString("http://example.com"));
cookieDict.Add (NSHttpCookie.KeyName, new NSString("Username"));
cookieDict.Add (NSHttpCookie.KeyValue, new NSString("Batman"));
cookieDict.Add (NSHttpCookie.KeyPath, new NSString("/"));

var myCookie = new NSHttpCookie(cookieDict);

NSHttpCookieStorage.SharedStorage.SetCookie(myCookie);

      



Any future requests will contain the cookie that you set in the shared store. So you can remove it in the future.

NSHttpCookieStorage.SharedStorage.DeleteCookie(myCookie);

      

Documentation on NSHTTPCookie and NSHttpCookieStorage:

  1. https://docs.microsoft.com/en-us/dotnet/api/foundation.nshttpcookiestorage?view=xamarin-ios-sdk-12

  2. https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSHTTPCookieStorage_Class/index.html

+6


source







All Articles