2016-01-21 94 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

好吧,不知道那:) –