简介:
Cookie是保存在浏览器一块用户信息,供服务器去访问
网上有三种方式注入Cookie:
第一种:将原先UIWebView里的Cookie数据资源拿出来,重新赋值给WKWebView,因为WKWebView里面有自己的Cookie管理类:WKHTTPCookieStore
if (@available(iOS 11.0, *)) {
WKHTTPCookieStore *cookieStore = self.webView.configuration.websiteDataStore.httpCookieStore;
for (NSHTTPCookie *cookie in cookies) {
//todo:添加domain
[cookieStore setCookie:cookie completionHandler:^{
NSLog(@"+++++");
}];
}
}else{
NSHTTPCookieStorage *shareCookie = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *cookie in cookies) {
[shareCookie setCookie:cookie];
}
}
第二种:向WKWebView的配置内容容器直接注入Cookie,以脚本语言的方式
WKUserScript *cookieScript = [[WKUserScript alloc] initWithSource:[self getCookieSource] injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
[self.webView.configuration.userContentController addUserScript:cookieScript];
第三种:在请求的URL里注入Cookie信息
NSURL *url = [NSURL URLWithString:[CHAIN_H5_DOMAIN_PATH stringByAppendingString:HOMEPAGEURL]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSArray *cookiesArray = [self get];
for (NSHTTPCookie *cookie in cookiesArray) {
NSDictionary *cookieDict = [cookie dictionaryWithValuesForKeys:@[NSHTTPCookieName,
NSHTTPCookieValue,
NSHTTPCookieDomain,
NSHTTPCookiePath]];
NSString *cookieStr = @"";
for (NSString *cookieKey in cookieDict.allKeys) {
NSString *keyValue = [NSString stringWithFormat:@"%@=%@;",cookieKey,[cookieDict objectForKey:cookieKey]];
cookieStr = [cookieStr stringByAppendingString:keyValue];
}
NSLog(@"本地取出的cookie:%@",cookieStr);
// cookie 写入request
[request addValue:cookieStr forHTTPHeaderField:@"Cookie"];
}
[self.webView loadRequest:request];
取出当前WebView的Cookie里面的数据:
- (NSMutableArray *)getCookieArrayFromWebView:(WKWebView *)webView{
NSMutableArray *cookiesArray = [NSMutableArray array];
if (@available(iOS 11.0, *)) {
WKHTTPCookieStore *cookieStore = webView.configuration.websiteDataStore.httpCookieStore;
[cookieStore getAllCookies:^(NSArray<NSHTTPCookie *> * cookies) {
[cookiesArray addObjectsFromArray:cookies];
NSData *cookiesData = [NSKeyedArchiver archivedDataWithRootObject:cookiesArray];
[[NSUserDefaults standardUserDefaults] setObject:cookiesData forKey:@"webCookie"];
[[NSUserDefaults standardUserDefaults] synchronize];
}];
} else {
NSArray *cookieArray = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
[cookiesArray addObjectsFromArray:cookieArray];
NSData *cookiesData = [NSKeyedArchiver archivedDataWithRootObject:cookiesArray];
[[NSUserDefaults standardUserDefaults] setObject:cookiesData forKey:@"webCookie"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
return cookiesArray;
}
同时呢,注入Cookie时也会遇到奇怪的问题:
- 首次注入Cookie失败: 以上面的第三种注入方式,在页面第一次打开时,发起请求前,去注入一次,同时以第二种方式注入脚本的方式再注入一次,保证注入后是成功的.
- 当webView有多个子页面时,侧滑手势可能会冲突:这个时候可以监听canGoback的属性,导航栏的页面层级数量去判断,是否禁用掉侧滑手势
侧滑手势冲突
[self.webView addObserver:self forKeyPath:@"canGoBack" options:NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew context:nil];
self.view.backgroundColor = [UIColor whiteColor];
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
BOOL oldValue = [[change objectForKey:@"old"] intValue];
BOOL newValue = [[change objectForKey:@"new"] intValue];
if (newValue) {
//当前页面级数 >=2
NSLog(@"有多级页面,支持侧滑");
}else{
NSLog(@"处于一级页面");
}
NSLog(@"旧值:%d,新值:%d-%@",oldValue,newValue,change);
}
- (void)dealloc{
[self.webView removeObserver:self forKeyPath:@"canGoBack" context:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}