Set Cookie for UIWebView requests

前端 未结 5 1775
抹茶落季
抹茶落季 2021-01-05 18:54

I want to embed an UIWebView into my MonoTouch application for an area that is not yet implemented natively.

In order to authenticate with the website I

5条回答
  •  独厮守ぢ
    2021-01-05 19:33

    AFAIK every application has its own cookie storage so try to use this code before rendering the page in the UIWebView

            NSHttpCookie cookie = new NSHttpCookie()
            {
                Domain = "yourdomain.com",
                Name = "YourName",
                Value = "YourValue" //and any other info you need to set
            };
            NSHttpCookieStorage cookiejar = NSHttpCookieStorage.SharedStorage;
            cookiejar.SetCookie(cookie);
    

    I'm not in a MAC right now so im not able to test it hope this helps


    okay sorry, i wasn't able to test it before posting, anyways I won't get home until tonight so give this a spin

    var objects = new object[] { "http://yoururl.com", "CookieName", "CookieValue", "/" };
    var keys = new object[] { "NSHTTPCookieOriginURL", "NSHTTPCookieName", "NSHTTPCookieValue", "NSHTTPCookiePath" };
    NSDictionary properties = (NSDictionary) NSDictionary.FromObjectsAndKeys(objects, keys);
    NSHttpCookie cookie = NSHttpCookie.CookieFromProperties(properties);
    NSHttpCookieStorage.SharedStorage.SetCookie(cookie);
    

    As you stated above, in the case that doesn't work might be a bug on monotouch binding so you can bind it manually by doing this

    var objects = new object[] { "http://yoururl.com", "CookieName", "CookieValue", "/" };
    var keys = new object[] { "NSHTTPCookieOriginURL", "NSHTTPCookieName", "NSHTTPCookieValue", "NSHTTPCookiePath" };
    NSDictionary properties = (NSDictionary) NSDictionary.FromObjectsAndKeys(objects, keys);
    NSHttpCookie cookie = (NSHttpCookie) Runtime.GetNSObject(Messaging.IntPtr_objc_msgSend_IntPtr(new Class("NSHTTPCookie").Handle, new Selector("cookieWithProperties:").Handle, properties.Handle))
    NSHttpCookieStorage.SharedStorage.SetCookie(cookie);
    

    also don't forget to include using MonoTouch.ObjCRuntime; if manually binding it

    if manually binding works please don't forget to post a bug report on https://bugzilla.xamarin.com/

    Alex

提交回复
热议问题