UIWebView not save cookies(UIWebView 不保存 cookie)
问题描述
我使用用户登录帐户的 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:UIWebView 不保存 cookie


基础教程推荐
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01