How to keep logged in session in UIWebview?

旧街凉风 提交于 2019-12-06 03:05:53

问题


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.

来源:https://stackoverflow.com/questions/26284862/how-to-keep-logged-in-session-in-uiwebview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!