未在UIWebView iOS中加载的Cookie

时间:2016-01-21 11:13:14

标签: ios objective-c cookies xamarin uiwebview

我有一个验证用户身份的UIWebView。用户通过身份验证后,我保存并删除co​​okie,如下所示: -

 public static void SaveCookies()
    {
        NSHttpCookieStorage cookieStorage = NSHttpCookieStorage.SharedStorage;
        foreach (var nscookie in cookieStorage.Cookies)
        {
            cookieStorage.SetCookie(nscookie);
        }


    }
    public static void DeleteCookies()
    {
        NSHttpCookieStorage cookieStorage = NSHttpCookieStorage.SharedStorage;
        foreach (var nscookie in cookieStorage.Cookies)
        {
            cookieStorage.DeleteCookie(nscookie);
        }


    }

我的AppDelegate中有这一行

public override void OnActivated (UIApplication application)
        {
            // Restart any tasks that were paused (or not yet started) while the application was inactive. 
            // If the application was previously in the background, optionally refresh the user interface.
           NSHttpCookieStorage.SharedStorage.AcceptPolicy = NSHttpCookieAcceptPolicy.Always;
        }

我这样称为SaveCookies:

var authResponse = REsponse after authentication
                 if (authResponse != null)
                 {
                    //SAve cookies here
                     AppDelegate.SaveCookies();
                    ...
                 }

但我不确定如何在下次应用启动时加载并将Cookie传递到webview,以便用户不会再次显示登录页面。现在,每个应用程序启动时都会显示“登录”页面。我想启用一次性登录。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

如果您使用iOS 9,Apple会引入名为SafariViewController的新控制器。该控制器负责处理所有cookie和缓存内容。以下是使用它的方法。

    SFSafariViewController * cntrl = [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString:@"http://yahoomail.com"] entersReaderIfAvailable:YES];
cntrl.delegate = self;
[self presentViewController:cntrl animated:YES completion:^{

}];

代替http://yahoomail.com以上通过您的网址。