【问题标题】:Cookies in web view xamarin iOSWeb 视图中的 Cookie xamarin iOS
【发布时间】:2015-06-28 08:48:25
【问题描述】:

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

【问题讨论】:

标签: xamarin xamarin.ios


【解决方案1】:

您需要在共享存储中设置 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

【讨论】:

    猜你喜欢
    • 2016-12-02
    • 2018-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多