网页视图中的Cookie xamarin iOS

Aja*_*ker 3 xamarin.ios xamarin

我想在Web视图中设置cookie.这有什么帮助吗?

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);
Run Code Online (Sandbox Code Playgroud)

小智 6

您需要在共享存储中设置cookie.首先,将共享存储策略设置为始终接受您自己的cookie.这可以放在ApplicationDelegate中(比如ApplicationDidBecomeActive).

NSHttpCookieStorage.SharedStorage.AcceptPolicy = NSHttpCookieAcceptPolicy.Always;
Run Code Online (Sandbox Code Playgroud)

创建cookie并将其设置为共享存储.

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);
Run Code Online (Sandbox Code Playgroud)

任何将来的请求都将包含您在共享存储中设置的cookie.所以你可能希望将来删除它.

NSHttpCookieStorage.SharedStorage.DeleteCookie(myCookie);
Run Code Online (Sandbox Code Playgroud)

有关NSHTTPCookie和NSHttpCookieStorage的文档:

  1. http://iosapi.xamarin.com/index.aspx?link=T%3AMonoTouch.Foundation.NSHttpCookie

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