2016-01-21 67 views
0

我有一个用于验证用户的UIWebView。用户通过验证后,我保存和删除cookies像这样: -Cookie未加载UIWebView iOS

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(); 
        ... 
       } 

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

+0

对此有任何好运..? – Ameer

回答

0

如果您使用的是iOS 9,苹果推出了新的控制器,名为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通过您的网址。

+0

嗨。谢谢回复。但IAM不使用iOS 9.我使用iOS 8.3 –

+0

好吧,不知道那:) –