WKWebView Cookie 加载cookie有两种方式
1、使用js注入的形式添加
2、使用请求头的形式添加
一使用js注入
//设置网页的配置文件
WKWebViewConfiguration * Configuration = [[WKWebViewConfiguration alloc]init];
//允许视频播放
Configuration.allowsAirPlayForMediaPlayback = YES;
// 允许在线播放
Configuration.allowsInlineMediaPlayback = YES;
// 允许可以与网页交互,选择视图
Configuration.selectionGranularity = YES;
// web内容处理池
Configuration.processPool = [[WKProcessPool alloc] init];
//自定义配置,一般用于 js调用oc方法(OC拦截URL中的数据做自定义操作)
WKUserContentController * UserContentController = [[WKUserContentController alloc]init];
// 添加消息处理,注意:self指代的对象需要遵守WKScriptMessageHandler协议,结束时需要移除
[UserContentController addScriptMessageHandler:self name:H1WebJS];
// 将所有cookie以document.cookie = 'key=value';形式进行拼接
// key=value key也就是你们后台定的接受参数。value就是常用的cookie的值,切记cookie的值是需要特殊编码(/ = 等)
NSString *cookieValue = [NSString stringWithFormat:@"document.cookie='userid=%@';", [PBUtility stringSpecialEncode:[UserObject shareUser].cookie]];
WKUserScript * cookieScript = [[WKUserScript alloc] initWithSource: cookieValue injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
[UserContentController addUserScript:cookieScript];
// 是否支持记忆读取
Configuration.suppressesIncrementalRendering = YES;
// 允许用户更改网页的设置
Configuration.userContentController = UserContentController;
_wkWebView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:Configuration];
使用js注入形式有个问题,请求第一个网页的时候,抓包发现没有看到userid=cookie的值,点击二级网页的时候携带了userid=cookie的值
二使用请求头形式
- (void)addCookieKey:(NSString *)cookiekey cookieValue:(NSString *)cookieValue cookieOriginURL:(NSString *)urlString
{
// 记得单独的cookievalue要特殊编码 = / 编码(也就是特殊字符)
NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
[cookieProperties setObject:cookiekey forKey:NSHTTPCookieName];
[cookieProperties setObject:[PBUtility stringSpecialEncode:cookieValue] forKey:NSHTTPCookieValue];
// cookie对应的url 没有的话NSHTTPCookie生成为nil 这里可以根据项目需求自己定义
[cookieProperties setObject:@"http://gr-share.com" forKey:NSHTTPCookieOriginURL];
// 设置cookie的范围路径
[cookieProperties setObject:@"/" forKey:NSHTTPCookiePath];
[cookieProperties setObject:@"0" forKey:NSHTTPCookieVersion];
// 将可变字典转化为cookie
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
}
- (void)loadRequestWithUrlString:(NSString *)urlString
{
//缓存web清除
[[NSURLCache sharedURLCache] removeAllCachedResponses];
// 保存需要的cookie
[self addCookieKey:@"userid" cookieValue:[UserObject shareUser].cookie cookieOriginURL:self.URLString];
// 在此处获取返回的cookie
NSMutableDictionary *cookieDic = [NSMutableDictionary dictionary];
NSMutableString *cookieValue = [NSMutableString string];
NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *cookie in [cookieJar cookies]) {
[cookieDic setObject:cookie.value forKey:cookie.name];
}
// cookie重复,先放到字典进行去重,再进行拼接
for (NSString *key in cookieDic) {
NSString *appendString = [NSString stringWithFormat:@"%@=%@;", key, [cookieDic valueForKey:key]];
[cookieValue appendString:appendString];
}
// 抓包其他web项目的写法cookie的最后一个不应该带有;
NSString *cookies = [cookieValue substringWithRange:NSMakeRange(0, cookieValue.length - 1)];
// cookies = [NSString stringWithFormat:@"userid=%@", [UserObject shareUser].cookie];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:15];
[request addValue:cookies forHTTPHeaderField:@"Cookie"];
//加载网页
[self.wkWebView loadRequest:request];
}
抓包发现,userid=xxx会在第一个请求的url的header里面会携带,进入二级页面没有看到userid=xxx。这个就需要看下问题了
可以好好测试了