问题描述
我在 iOS 应用程序中有 webviews,需要验证 cookie 才能正确验证.我正在寻找一种在 iOS 应用程序的 web 视图中设置 cookie 的方法,而无需发出出站请求来设置 cookie,因为我已经在客户端上拥有了身份验证信息.
此帖子向我们展示了 UIWebView cookie 的存储位置.p>
现在我正在加载一个隐藏的 Web 视图来发出出站请求,但我希望不必发出外部请求来设置简单的 cookie.
是的,你可以这样做.首先,在 applicationDidBecomeActive 添加这一行
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
cookieAcceptPolicy 是跨应用共享的,并且可以在您不知情的情况下进行更改,因此您需要确保每次应用运行时都拥有所需的接受策略.
然后,设置cookie:
NSMutableDictionary *cookieProperties = [NSMutableDictionary 字典];[cookieProperties setObject:@"testCookie" forKey:NSHTTPCookieName];[cookieProperties setObject:@"someValue123456" forKey:NSHTTPCookieValue];[cookieProperties setObject:@"www.example.com" forKey:NSHTTPCookieDomain];[cookieProperties setObject:@"www.example.com" forKey:NSHTTPCookieOriginURL];[cookieProperties setObject:@"/" forKey:NSHTTPCookiePath];[cookieProperties setObject:@"0" forKey:NSHTTPCookieVersion];//将过期时间设置为从现在起一个月或您选择的任何 NSDate//这使得 cookie 是无会话的,它将在 web 会话和应用程序启动中持续存在///如果您希望在您的应用退出时销毁 cookie,请不要设置此项[cookieProperties setObject:[[NSDate date] dateByAddingTimeInterval:2629743] forKey:NSHTTPCookieExpires];NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
此 cookie 的名称为 testCookie,值为 someValue123456,将与任何 http 请求一起发送到 www.example.com.
关于设置 cookie 的一大注意事项,请在此处查看我的问题!
NSHTTPCookieStorage 状态未在应用退出时保存.有任何权威的知识/文档吗?
I have webviews inside of an iOS application that require an authentication cookie to be properly authenticated. I'm looking for a way to set a cookie inside of an iOS application's webview without having to make an outbound request to set the cookie as I have auth information on the client already.
This post shows us where the UIWebView cookies are stored.
Right now I'm loading a hidden web view to make an outbound request but would prefer not to have to make an external request for setting a simple cookie.
Yes, you can do this. First, in applicationDidBecomeActive add this line
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
The cookieAcceptPolicy is shared across apps and can be changed without your knowledge, so you want to be sure you have the accept policy you need every time your app is running.
Then, to set the cookie:
NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
[cookieProperties setObject:@"testCookie" forKey:NSHTTPCookieName];
[cookieProperties setObject:@"someValue123456" forKey:NSHTTPCookieValue];
[cookieProperties setObject:@"www.example.com" forKey:NSHTTPCookieDomain];
[cookieProperties setObject:@"www.example.com" forKey:NSHTTPCookieOriginURL];
[cookieProperties setObject:@"/" forKey:NSHTTPCookiePath];
[cookieProperties setObject:@"0" forKey:NSHTTPCookieVersion];
// set expiration to one month from now or any NSDate of your choosing
// this makes the cookie sessionless and it will persist across web sessions and app launches
/// if you want the cookie to be destroyed when your app exits, don't set this
[cookieProperties setObject:[[NSDate date] dateByAddingTimeInterval:2629743] forKey:NSHTTPCookieExpires];
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
This cookie has the name testCookie and value someValue123456 and will be sent with any http request to www.example.com.
For one big caveat to setting cookies, please see my question here!
NSHTTPCookieStorage state not saved on app exit. Any definitive knowledge/documentation out there?
这篇关于是否可以使用 sharedHTTPCookieStorage 为 UIWebView 手动设置 cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!