Web 视图中的 Cookie xamarin iOS [英] Cookies in web view xamarin iOS
问题描述
我想在网络视图中设置 cookie.对此有什么帮助吗?
I want to set cookies in web view. Is there any help regarding this?
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).
You need to set the cookie in the shared storage. Firstly, set your shared storage policy to always accept your own cookies. This can be placed in your ApplicationDelegate (say ApplicationDidBecomeActive).
NSHttpCookieStorage.SharedStorage.AcceptPolicy = NSHttpCookieAcceptPolicy.Always;
创建您的 cookie 并将其设置为共享存储.
Create your cookie and set it to the 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);
以后的任何请求都将包含您在共享存储中设置的 cookie.所以你以后可能想删除它.
Any future requests will contain the cookie you have set in the shared storage. So you may want to delete it in the future.
NSHttpCookieStorage.SharedStorage.DeleteCookie(myCookie);
关于 NSHTTPCookie 和 NSHttpCookieStorage 的文档:
Documentation on NSHTTPCookie and NSHttpCookieStorage:
这篇关于Web 视图中的 Cookie xamarin iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!