How to keep logged in session in UIWebview?

2019-04-08 23:38发布

I am using UIWebView in my app. I want to load a a URL on first webview, from that user can login his account. After login then User came out of the webview and do other stuffs in iOS native views. Later when he call the same URL, he must be logged in. How is it possible ? I hav seen this in Gmail when I logged in my account in one tab, and close it my GMAIl account is logged in safari whenever I open any tab.

I have tried something below, but not working. Please suggest me a solution

   -(void)loadCookies
   {
   NSArray  *cookies       = [NSKeyedUnarchiver unarchiveObjectWithData: [[NSUserDefaults standardUserDefaults] objectForKey: @"cookies"]];
   NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];

   for (NSHTTPCookie *cookie in cookies)
  {
    [cookieStorage setCookie: cookie];
   }
   }



 -(void)saveCookies
  {
NSData         *cookiesData = [NSKeyedArchiver archivedDataWithRootObject:       [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]];
NSUserDefaults *defaults    = [NSUserDefaults standardUserDefaults];
[defaults setObject: cookiesData forKey: @"cookies"];
[defaults synchronize];
 }

My apps flow is like below.

1.Calling a URL on a UIWebview - http://www.userlogin.com 2.Poping from that view controller 3.Doing other features in the app. 4.Calling same URL in different UIWebView - http://www.userlogin.com

Here I need to show his logged in session.

0条回答
登录 后发表回答