0

I use UIWebView that users login in accounts. Users may login with facebook account. He is click button Facebook and opens UIWebView. After login UIWebView close and users may use your personal account. But when I close my app and open it again users not login. UIWebView not save cookies. I found this answer https://stackoverflow.com/a/26006163 And added this code in my app. This only works temporarily. I close my app and open through hours it again users not login. I tried to change this line

[cookieProperties setObject:[[NSDate date] dateByAddingTimeInterval:2629743] forKey:NSHTTPCookieExpires];

to this

[cookieProperties setObject:[[NSDate date] dateByAddingTimeInterval:100*12*30*60*60] forKey:NSHTTPCookieExpires];

But it did not help me.

Community
  • 1
  • 1

2 Answers2

1

Cookies are temporary and it doest miraculously come back when you relaunch the app.

you need to save the cookies or the credential in keychain and get it back once you relaunch.

cocoa bean
  • 21
  • 3
  • 1
    http://stackoverflow.com/questions/16459879/how-to-store-a-string-in-keychain-ios have a look at it – cocoa bean Feb 28 '15 at 09:26
  • after one hour cookies removed again :( How to save cookies forever? How to solve this problem? –  Feb 28 '15 at 12:13
  • In Facebook cookies save forever, but in site cookies save temporary. In Safari cookies save forever –  Feb 28 '15 at 14:30
0

I had such a problem. I tried many ways. I decided use dirty hack :D That's my way:

When I was getting NSHTTPURLResponse for facebook (or else) i save request url to NSUserDefaults:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
    if ([[httpResponse URL].absoluteString isEqualToString:@"http://www.simple.com/"])
    {
        [[NSUserDefaults standardUserDefaults] setURL:self.url forKey:@"urlLogin"];
        [self dismissViewController];
    }
}

And when I open my App i use NSURLRequest with my stored url:

NSURLRequest *request = [NSURLRequest requestWithURL:[[NSUserDefaults standardUserDefaults] URLForKey:@"urlLogin"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:3.0];
[NSURLConnection connectionWithRequest:request delegate:self];
runia
  • 340
  • 4
  • 18
  • It really helped me! Only bad thing is that it ships a few extra kilobytes, but this dirty hack works :D –  Feb 28 '15 at 20:13