我在iOS应用程序内部具有Webview,这些Webview要求对身份验证cookie进行正确的身份验证。我正在寻找一种在iOS应用程序的Webview中设置cookie的方法,而不必发出出站请求来设置cookie,因为我已经在客户端上拥有身份验证信息。
这篇文章向我们展示了UIWebView cookie的存储位置。
现在,我正在加载一个隐藏的Web视图以发出出站请求,但希望不必通过外部请求来设置简单的Cookie。
Answers:
是的,您可以这样做。首先,在applicationDidBecomeActive中添加此行
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
cookieAcceptPolicy在应用程序之间共享,并且可以在不知情的情况下进行更改,因此,您需要确保每次应用程序运行时都具有所需的接受策略。
然后,设置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];
此Cookie的名称为testCookie,值someValue123456,并将与任何http请求一起发送到www.example.com。
有关设置Cookie的一个大警告,请在此处查看我的问题!
编辑:适应已编辑的问题
NSHTTPCookieStorage
有一个-setCookies:forURL:mainDocumentURL:
方法,因此最简单的方法是使用NSURLConnection
和实现-connection:didReceiveResponse:
,提取cookie并将其填充到cookie jar中:
- ( void )connection: (NSURLConnection *)connection
didReceiveResponse: (NSURLResponse *)response
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
NSArray *cookies;
cookies = [ NSHTTPCookie cookiesWithResponseHeaderFields:
[ httpResponse allHeaderFields ]];
[[ NSHTTPCookieStorage sharedHTTPCookieStorage ]
setCookies: cookies forURL: self.url mainDocumentURL: nil ];
}
(你也可以简单地提取NSDictionary
从对象NSHTTPCookie
用properties
,然后写字典中的磁盘。读它放回是那么容易,因为使用NSDictionary
的-dictionaryWithContentsOfFile:
,然后创建与cookie-initWithProperties:
)。
然后,您可以在需要时将Cookie从存储中拉回:
- ( void )reloadWebview: (id)sender
{
NSArray *cookies;
NSDictionary *cookieHeaders;
NSMutableURLRequest *request;
cookies = [[ NSHTTPCookieStorage sharedHTTPCookieStorage ]
cookiesForURL: self.url ];
if ( !cookies ) {
/* kick off new NSURLConnection to retrieve new auth cookie */
return;
}
cookieHeaders = [ NSHTTPCookie requestHeaderFieldsWithCookies: cookies ];
request = [[ NSMutableURLRequest alloc ] initWithURL: self.url ];
[ request setValue: [ cookieHeaders objectForKey: @"Cookie" ]
forHTTPHeaderField: @"Cookie" ];
[ self.webView loadRequest: request ];
[ request release ];
}
在Swift 3中,所有键都包装在HTTPCookiePropertyKey
struct中:
let cookieProperties: [HTTPCookiePropertyKey : Any] = [.name : "name",
.value : "value",
.domain : "www.example.com",
.originURL : "www.example.com",
.path : "/",
.version : "0",
.expires : Date().addingTimeInterval(2629743)
]
if let cookie = HTTPCookie(properties: cookieProperties) {
HTTPCookieStorage.shared.setCookie(cookie)
}
domain
和originURL