UIWebView not save cookies

2019-04-17 06:58发布

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.

2条回答
老娘就宠你
2楼-- · 2019-04-17 07:31

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.

查看更多
孤傲高冷的网名
3楼-- · 2019-04-17 07:35

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];
查看更多
登录 后发表回答