可能这是一个愚蠢的问题,但我想为相同的 url 存储 cookie,但为用户名存储不同的 cookie。如何使用 NSHTTPCookieStorage 来实现?这就是我从响应中存储 cookie 的方式。
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
NSDictionary *headerFields = [httpResponse allHeaderFields];
NSArray* cookies = [NSHTTPCookie
cookiesWithResponseHeaderFields:headerFields
forURL:[NSURL URLWithString:@""]];
for (NSHTTPCookie *cookie in cookies) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
}
NSString *urlString = ...;
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:cookies forURL:[NSURL URLWithString:urlString] mainDocumentURL:nil];