UIWebView not save cookies
Asked Answered
D

2

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.

Dowlen answered 28/2, 2015 at 8:53 Comment(0)
Z
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];
Zielsdorf answered 28/2, 2015 at 19:52 Comment(1)
It really helped me! Only bad thing is that it ships a few extra kilobytes, but this dirty hack works :DDowlen
M
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.

Modulate answered 28/2, 2015 at 9:23 Comment(3)
#16460379 have a look at itModulate
after one hour cookies removed again :( How to save cookies forever? How to solve this problem?Dowlen
In Facebook cookies save forever, but in site cookies save temporary. In Safari cookies save foreverDowlen
Z
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];
Zielsdorf answered 28/2, 2015 at 19:52 Comment(1)
It really helped me! Only bad thing is that it ships a few extra kilobytes, but this dirty hack works :DDowlen

© 2022 - 2024 — McMap. All rights reserved.