网络视图中的 Cookie xamarin iOS

Cookies in web view xamarin iOS

我想在网络视图中设置 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);

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

NSHttpCookieStorage.SharedStorage.AcceptPolicy = NSHttpCookieAcceptPolicy.Always;

创建您的 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);

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

NSHttpCookieStorage.SharedStorage.DeleteCookie(myCookie);

关于 NSHTTPCookie 和 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