0
votes

My UIWebview is not saving cookies at all. It is stuck in cookieless mode and as a result, the webpage is putting the cookie information in the url instead.

Here is what I am doing. I have a singleton for my UIWebview and 4 UITabBar buttons on the bottom. Each tabbar button takes the user to a different page on the site. Now, when the user is navigating the site through the webview itself, everything is fine and session persists. But the second a user clicks on a tabbar button, the session gets reset.

I even set NSHttpCookieStorage to always accept cookies. Still no go.

Here is the code for my singleton for UIWebview and NSMutableUrlRequest

    public static UIWebView instance;
    public static NSMutableUrlRequest urlRequest;
    public static NSUrlConnection connection;
    public static NSHttpCookieStorage cookie;
    static bool TokenSent = false;
    public UIWebViewSingleton () {}

    public static UIWebView Instance
    {
        get
        {
            if (instance == null)
            {   
                Debugger.Debug ("new uiwebview created");
                cookie = NSHttpCookieStorage.SharedStorage;
                cookie.AcceptPolicy = NSHttpCookieAcceptPolicy.Always;
                connection = new NSUrlConnection(); 
                instance = new UIWebView(new RectangleF(0f, 0f, 320f, 416f));
                instance.ScalesPageToFit = true;
                instance.LoadStarted += delegate {
                    UIApplication.SharedApplication.NetworkActivityIndicatorVisible = true;
                };
                instance.LoadFinished += delegate {
                    UIApplication.SharedApplication.NetworkActivityIndicatorVisible = false;
                    AcquireUserId();
                };
                instance.MultipleTouchEnabled = false;
            }
            return instance;
        }
    }

    public static NSMutableUrlRequest UrlRequest
    {
        get
        {
            if (urlRequest == null)
            {
                urlRequest = new NSMutableUrlRequest();
            }
            return urlRequest;
        }
    }

This is how I change to a page based on which button was pressed. The back button is working fine.

        case BTN_BACK:
            btn_ret.TouchUpInside += delegate(object sender, EventArgs e) {
                UIWebViewSingleton.Instance.GoBack();       
            };
            break;

        case BTN_GUIDE:
            btn_ret.TouchUpInside += delegate(object sender, EventArgs e) {
                UIWebViewSingleton.UrlRequest.Url = new NSUrl(StaticFileNames.GuideUrl);
                UIWebViewSingleton.Instance.LoadRequest (UIWebViewSingleton.UrlRequest);        
            };
            break;

        case BTN_HOME:
            btn_ret.TouchUpInside += delegate(object sender, EventArgs e) {
                UIWebViewSingleton.UrlRequest.Url = new NSUrl(StaticFileNames.BaseUrl);
                UIWebViewSingleton.Instance.LoadRequest (UIWebViewSingleton.UrlRequest);        
            };
            break;

Does anyone have any ideas as to why my UIWebView is not accepting cookies? I'm still confused about NSHttpCookieStorage and if I'm using that correctly.

Thanks a lot.

1
Are they persistent cookies and not session cookies?Gruntcakes
I can check it out in chrome and I get the cookie properly.apexdodge

1 Answers

0
votes

I went with this work-around which was sufficient.

Since the cookie is in the url, I just parse it out and put programmatically put it into the urls that the tabbar buttons point to. Problem solved.