是否可以在 Objective C 中的现有 cookie 中添加值
Is it possible to add values in an existing cookie in Objective C
我想在加载 webview URL 之前在现有 cookie 中添加三个额外的值,如 firstname、lastname 和 title,这样用户就不必在 webview 中再次登录。是否可以在Objective C 中的现有 cookie ?
是的,您可以使用 NSHTTPCookie
和 NSHTTPCookieStorage
添加自己的 cookie
1) 创建字典来设置 cookie 的属性,例如(以下是 cookie 的几个属性):
NSMutableDictionary *dictCookieID = [NSMutableDictionary dictionary];
[dictCookieID setObject:value forKey:NSHTTPCookieName];
[dictCookieID setObject:value forKey:NSHTTPCookieValue];
[dictCookieID setObject:value forKey:NSHTTPCookieDomain];
[dictCookieID setObject:value forKey:NSHTTPCookiePath];
[dictCookieID setObject:value forKey:NSHTTPCookieVersion];
2) 创建 NSHTTPCookie
的对象
NSHTTPCookie *cookieID = [NSHTTPCookie dictCookieID];
3) 存储 cookie:
NSArray *cookies = [NSArray arrayWithObjects: cookieID,nil]; // You can add multiple cookies in this array
[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookieID];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
4) 加载 url 到 webview:
NSString* url = URL_NAME;
NSURL* nsUrl = [NSURL URLWithString:url];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:nsUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:];
[request setHTTPShouldHandleCookies:YES];
NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
[(NSMutableURLRequest *)request setAllHTTPHeaderFields:headers];
[webView loadRequest:request];
使用 NSURLSession
您可以在 NSHTTPCookieStorage
中设置 cookie。但是,cookie 只是一个您可以设置的 header 字段。
我想在加载 webview URL 之前在现有 cookie 中添加三个额外的值,如 firstname、lastname 和 title,这样用户就不必在 webview 中再次登录。是否可以在Objective C 中的现有 cookie ?
是的,您可以使用 NSHTTPCookie
和 NSHTTPCookieStorage
1) 创建字典来设置 cookie 的属性,例如(以下是 cookie 的几个属性):
NSMutableDictionary *dictCookieID = [NSMutableDictionary dictionary];
[dictCookieID setObject:value forKey:NSHTTPCookieName];
[dictCookieID setObject:value forKey:NSHTTPCookieValue];
[dictCookieID setObject:value forKey:NSHTTPCookieDomain];
[dictCookieID setObject:value forKey:NSHTTPCookiePath];
[dictCookieID setObject:value forKey:NSHTTPCookieVersion];
2) 创建 NSHTTPCookie
NSHTTPCookie *cookieID = [NSHTTPCookie dictCookieID];
3) 存储 cookie:
NSArray *cookies = [NSArray arrayWithObjects: cookieID,nil]; // You can add multiple cookies in this array
[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookieID];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
4) 加载 url 到 webview:
NSString* url = URL_NAME;
NSURL* nsUrl = [NSURL URLWithString:url];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:nsUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:];
[request setHTTPShouldHandleCookies:YES];
NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
[(NSMutableURLRequest *)request setAllHTTPHeaderFields:headers];
[webView loadRequest:request];
使用 NSURLSession
您可以在 NSHTTPCookieStorage
中设置 cookie。但是,cookie 只是一个您可以设置的 header 字段。