问题描述
我使用用户登录帐户的 UIWebView.用户可以使用 facebook 帐户登录.他是点击按钮 Facebook 并打开 UIWebView.登录后 UIWebView 关闭,用户可以使用您的个人帐户.但是当我关闭我的应用程序并再次打开它时,用户没有登录.UIWebView 不保存 cookie.我找到了这个答案 https://stackoverflow.com/a/26006163并在我的应用程序中添加了此代码.这只是暂时的.我关闭了我的应用程序并在几个小时内再次打开它,用户没有登录.我试图改变这一行
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];
到这里
[cookieProperties setObject:[[NSDate date] dateByAddingTimeInterval:100*12*30*60*60] forKey:NSHTTPCookieExpires];
但这对我没有帮助.
推荐答案
我遇到了这样的问题.我尝试了很多方法.我决定使用肮脏的黑客:D这就是我的方式:
I had such a problem. I tried many ways. I decided use dirty hack :D That's my way:
当我为 facebook(或其他)获取 NSHTTPURLResponse 时,我将请求 url 保存到 NSUserDefaults:
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];
}
}
当我打开我的应用程序时,我将 NSURLRequest 与我存储的 url 一起使用:
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];
这篇关于UIWebView 不保存 cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!