在iOS8之后蘋果推出了WKWebView控件友扰,用來顯示網(wǎng)頁,以逐漸淘汰之前的UIWebView控件赚爵。雖然在加載速度和內(nèi)存使用等方面WKWebView遠遠勝過了UIWebView棉胀,但是WKWebView在加載網(wǎng)頁的時候,給網(wǎng)頁發(fā)送Cookie的時候總?cè)菀讈G失冀膝,就會造成網(wǎng)頁無法拿到移動端發(fā)送過去的Cookie唁奢,從而會引發(fā)bug。經(jīng)過多種途徑的實驗窝剖,最終采用GGWkCookie第三方來發(fā)送Cookie麻掸。
Cookie是什么?
Cookie是由服務(wù)器保存在客戶端上的一塊數(shù)據(jù)赐纱。它包含著相關(guān)用戶的信息脊奋,比如果用戶的登陸狀態(tài)、用戶標識等等疙描。
Cookie有什么作用诚隙?
cookie的作用主要體現(xiàn)在以下的三個方面:
1.會話狀態(tài)管理(如用戶登錄狀態(tài)、購物車)起胰;
2.個性化設(shè)置(如用戶自定義設(shè)置);
3.瀏覽器行為跟蹤(如跟蹤分析用戶行為)久又。
Cookie的處理步驟:
1.服務(wù)器向客戶端發(fā)送Cookie;
2.通常使用HTTP協(xié)議規(guī)定的Set-Cookie頭操作效五;
3.規(guī)范規(guī)定Cookie的格式為 name = value 格式地消,且必須包含這部分;
4.客戶端將Cookie保存畏妖;
5.每次請求客戶端都會將Cookie發(fā)送給服務(wù)器脉执。
Cookie長什么樣子?
當(dāng)服務(wù)器收到HTTP請求時戒劫,可以在響應(yīng)頭里面增加一個Set-Cookie頭部半夷。客戶端收到響應(yīng)之后會取出Cookie信息并保存谱仪,之后對該服務(wù)器每一次請求中都通過Cookie請求頭部將Cookie信息發(fā)送給服務(wù)器玻熙。大概長的都是這個格式:
Set-Cookie: <cookie名稱>=<cookie值>
所以一個簡單的Cookie就如下所示:
language=zh_CN; expires=Sat, 05-Aug-2017 08:21:16 GMT; Max-Age=2592000; path=/; domain=192.75.17.211:6603
當(dāng)在設(shè)置Cookie的時候回用到下面的幾個類:
- NSHTTPCookieStorage:這個類就是一個單例,它的主要任務(wù)就是管理 Cookie疯攒,用來做增刪改查等各種操作的嗦随。
- NSURLRequest:這個類是HTTP請求協(xié)議URL資源的消息對象Request;
- NSHTTPURLResponse:這個類是HTTP協(xié)議請求URL資源的響應(yīng)消息對象。這個對象將HTTP協(xié)議的序列化了枚尼,可以很方便的獲得狀態(tài)碼(statusCode)贴浙,消息報頭(allHeaderFields)等信息。
在代碼中可以使用以下的兩種方式來獲取Cookie:
1.從NSHTTPURLResponse獲取服務(wù)器發(fā)給我們的Cookie署恍。這種方式獲取的是Headers中的Cookie崎溃。
NSHTTPURLResponse* response = (NSHTTPURLResponse* )task.response;
NSDictionary *allHeaderFieldsDic = response.allHeaderFields;
NSString *setCookie = allHeaderFieldsDic[@"Set-Cookie"];
if (setCookie != nil)
{
NSString *cookie = [[setCookie componentsSeparatedByString:@";"] objectAtIndex:0];
NSLog(@"cookie : %@", cookie); // 這里可對cookie進行存儲
}
2.從 NSHTTPCookieStorage 獲取想要的Cookie。此種獲取方式是獲取的cookies中的盯质。
NSArray <NSHTTPCookie *>*cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:self.webUrlStr]];
NSLog(@"cookies = %@", cookies);
for (NSHTTPCookie *tempCookie in cookies)
{
NSLog(@"tempCookie = %@", tempCookie);
NSLog(@"cookieName = %@, cookieValue = %@", tempCookie.name, tempCookie.value);
}
清除Cookie:
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray *_tmpArray = [NSArray arrayWithArray:[cookieStorage cookies]];
for (id obj in _tmpArray)
{
[cookieStorage deleteCookie:obj];
}
在自己的項目中利用WKWebView來加載網(wǎng)頁袁串,需要在加載的時候向網(wǎng)頁傳遞Cookie,以便網(wǎng)頁端拿到Cookie之后做一些其他的操作呼巷,所以就用了如下的兩種方式向網(wǎng)頁傳遞Cookie:
1.直接設(shè)置方式:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.webUrlStr]];
//獲取Cookie
NSString *cookieString = [EHLCookieUtils getCookieString];
NSLog(@"cookieString = %@", cookieString);
[request addValue:cookieString forHTTPHeaderField:@"Cookie"];
[self.webView loadRequest:request];
利用Charles截包工具在加載網(wǎng)頁的時候查看囱修,沒有將Cookie發(fā)送出去,猜測可能是在發(fā)送的時候丟失了Cookie王悍。
2.JS注入方式:
//獲取Cookie
NSString *cookieString = [EHLCookieUtils getCookieString];
NSLog(@"cookieString = %@", cookieString);
NSString *cookieStr = [NSString stringWithFormat:@"document.cookie ='%@';",cookieString];
NSLog(@"cookieStr = %@", cookieStr);
WKUserScript *cookieScript = [[WKUserScript alloc] initWithSource: cookieStr injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
[_userContentController addUserScript:cookieScript];
在利用Charles截包工具在加載網(wǎng)頁的時候查看破镰,沒有將Cookie發(fā)送出去,猜測可能是在發(fā)送的時候丟失了Cookie压储。
在上述方式都失敗的情況下選擇使用GGWkCookie第三方來發(fā)送Cookie鲜漩,經(jīng)實驗發(fā)送成功。
WKWebview支持的插入腳本的方式集惋,在每次頁面渲染前孕似,通過插入的Js腳本檢測Cookie是否存在,如不存在芋膘,將Cookie重新種入的思路鳞青。
GGWkCookie github地址:https://github.com/GaoGuohao/GGWkCookie
在GGWkCookie的代理方法中撰寫如下的代碼:
#pragma mark -------------- GGWkWebViewDelegate --------------
- (NSDictionary *)webviewSetAppCookieKeyAndValue
{
//從 NSHTTPCookieStorage 獲取想要的Cookie霸饲,此種獲取方式是獲取的cookies中的
NSArray <NSHTTPCookie *>*cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:self.webUrlStr]];
NSLog(@"cookies = %@", cookies);
NSHTTPCookie *cookie = nil;
for (NSHTTPCookie *tempCookie in cookies)
{
NSLog(@"tempCookie = %@", tempCookie);
NSLog(@"cookieName = %@, cookieValue = %@", tempCookie.name, tempCookie.value);
cookie = tempCookie;
}
NSDictionary *requestDic = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
NSLog(@"requestDic = %@", requestDic);
NSString *cookie1 = [requestDic objectForKey:@"Cookie"];
NSLog(@"cookie1 = %@", cookie1);
NSDictionary *cookieDic = [NSDictionary dictionaryWithObject:cookie.value forKey:cookie.name];
NSLog(@"cookieDic = %@", cookieDic);
return cookieDic;
}
在本人的項目中這個代理方法會被調(diào)用兩次为朋。根據(jù)GGWkCookie的原理,在每次頁面渲染前厚脉,通過插入的Js腳本檢測Cookie是否存在习寸,如不存在,則將cookie重新種入傻工。第一次的時候沒有種入成功霞溪,所以就再次進行了調(diào)用,再次種入中捆,這就是調(diào)用兩次的原因鸯匹。
再看運行結(jié)果:
2020-09-26 02:05:24.872077+0800 葫蘆[20198:281532] cookies = (
"<NSHTTPCookie\n\tversion:0\n\tname:SESSION\n\tvalue:ZjIzMmE2ODMtMzY0My00MjEwLTljMmEtMGU1NzJkNWM2ZjFh\n\texpiresDate:'(null)'\n\tcreated:'2020-09-25 13:53:23 +0000'\n\tsessionOnly:TRUE\n\tdomain:service-bak.hulucc.com\n\tpartition:none\n\tsameSite:none\n\tpath:/\n\tisSecure:TRUE\n path:\"/\" isSecure:TRUE>"
)
一開始獲取到的cookies是由NSHTTPCookie類的對象(cookie)所組成的數(shù)組,所以叫cookies泄伪。
2020-09-26 02:05:24.872257+0800 葫蘆[20198:281532] tempCookie = <NSHTTPCookie
version:0
name:SESSION
value:ZjIzMmE2ODMtMzY0My00MjEwLTljMmEtMGU1NzJkNWM2ZjFh
expiresDate:'(null)'
created:'2020-09-25 13:53:23 +0000'
sessionOnly:TRUE
domain:service-bak.hulucc.com
partition:none
sameSite:none
path:/
isSecure:TRUE
path:"/" isSecure:TRUE>
這個cookies數(shù)組里面只有一個元素殴蓬,即:NSHTTPCookie類型的對象(cookie),把這個cookie對象打印出來可以知道蟋滴,這個NSHTTPCookie類有好多種屬性染厅,其中最重要的是name屬性和value屬性痘绎。
2020-09-26 02:05:24.872343+0800 葫蘆[20198:281532] cookieName = SESSION, cookieValue = ZjIzMmE2ODMtMzY0My00MjEwLTljMmEtMGU1NzJkNWM2ZjFh
2020-09-26 02:05:24.872477+0800 葫蘆[20198:281532] requestDic = {
Cookie = "SESSION=ZjIzMmE2ODMtMzY0My00MjEwLTljMmEtMGU1NzJkNWM2ZjFh";
}
2020-09-26 02:05:24.872558+0800 葫蘆[20198:281532] cookie1 = SESSION=ZjIzMmE2ODMtMzY0My00MjEwLTljMmEtMGU1NzJkNWM2ZjFh
最后在這個代理方法中要返回的字典對象為:
2020-09-26 02:05:24.872729+0800 葫蘆[20198:281532] cookieDic = {
SESSION = ZjIzMmE2ODMtMzY0My00MjEwLTljMmEtMGU1NzJkNWM2ZjFh;
}
整個過程結(jié)束,網(wǎng)頁可以直接拿到客戶端發(fā)過來的Cookie肖粮,然后進行接下來的操作孤页。
迷思釋惑
1.在GGWkCookie的代理方法中用如下的方式獲取Cookie:
NSArray<NSHTTPCookie *> *cookies2 = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
#pragma mark -------------- GGWkWebViewDelegate --------------
- (NSDictionary *)webviewSetAppCookieKeyAndValue
{
NSArray<NSHTTPCookie *> *cookies2 = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
for (NSHTTPCookie *c in cookies2)
{
NSLog(@"$$$$$$$$$$$$$c = %@", c);
}
// NSString *cookieString = [EHLCookieUtils getCookieString];
// NSLog(@"cookieString = %@", cookieString);
//從 NSHTTPCookieStorage 獲取想要Cookie,此種獲取方式是獲取的cookies中的
NSArray <NSHTTPCookie *>*cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:self.webUrlStr]];
NSLog(@"cookies = %@", cookies);
NSHTTPCookie *cookie = nil;
for (NSHTTPCookie *tempCookie in cookies)
{
NSLog(@"tempCookie = %@", tempCookie);
NSLog(@"cookieName = %@, cookieValue = %@", tempCookie.name, tempCookie.value);
cookie = tempCookie;
}
NSDictionary *requestDic = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
NSLog(@"requestDic = %@", requestDic);
NSString *cookie1 = [requestDic objectForKey:@"Cookie"];
NSLog(@"cookie1 = %@", cookie1);
NSDictionary *cookieDic = [NSDictionary dictionaryWithObject:cookie.value forKey:cookie.name];
NSLog(@"cookieDic = %@", cookieDic);
return cookieDic;
}
逐個打印用上述方式獲取到的Cookies數(shù)組里面的元素可以知道涩馆,一共有三個NSHTTPCookie的對象行施,打印結(jié)果為:
2020-09-26 02:29:16.344962+0800 葫蘆[20696:306124] $$$$$$$$$$$$$c = <NSHTTPCookie
version:0
name:SESSION
value:ZjIzMmE2ODMtMzY0My00MjEwLTljMmEtMGU1NzJkNWM2ZjFh
expiresDate:'(null)'
created:'2020-09-25 13:53:23 +0000'
sessionOnly:TRUE
domain:service-bak.hulucc.com
partition:none
sameSite:none
path:/
isSecure:TRUE
path:"/" isSecure:TRUE>
2020-09-26 02:29:17.187079+0800 葫蘆[20696:306124] $$$$$$$$$$$$$c = <NSHTTPCookie
version:0
name:JSESSIONID
value:149591F5BF45FD9689768C475437BD49
expiresDate:'(null)'
created:'2020-09-25 18:29:06 +0000'
sessionOnly:TRUE
domain:service-bak.hulucc.com
partition:none
sameSite:none
path:/calabash
isSecure:FALSE
isHTTPOnly: YES
path:"/calabash" isSecure:FALSE isHTTPOnly: YES>
2020-09-26 02:29:18.012728+0800 葫蘆[20696:306124] $$$$$$$$$$$$$c = <NSHTTPCookie
version:0
name:SESSION
value:ZjVhYzUyZWYtZGQ1NS00NDVhLTgyMDUtOGVkZGM2ZjZmOWYw
expiresDate:'(null)'
created:'2020-09-25 13:53:23 +0000'
sessionOnly:TRUE
domain:service-bak.hulucc.com
partition:none
sameSite:lax
path:/gateway
isSecure:FALSE
isHTTPOnly: YES
path:"/gateway" isSecure:FALSE isHTTPOnly: YES>
其實真正有效的Cookie是第一個。因為每個NSHTTPCookie對象中都有一個isSecure屬性魂那,當(dāng)這個屬性為TRUE的時候悲龟,則意味著此 cookie 在HTTP中是無效的,而在HTTPS中才有效冰寻。當(dāng)這個屬性為FALSE的時候须教,則意味著此Cookie不管在HTTP還是在HTTPS中都是無效的。所以第二個和第三個Cookie是無效的斩芭。
2.在GGWkCookie的代理方法中用如下的方式獲取Cookie:
NSString *cookieString = [EHLCookieUtils getCookieString];
NSLog(@"cookieString : %@", cookieString);
上面的getCookieString方法是本項目中其他人寫的一個獲取Cookies的方法轻腺。
#pragma mark -------------- GGWkWebViewDelegate --------------
- (NSDictionary *)webviewSetAppCookieKeyAndValue
{
// NSArray<NSHTTPCookie *> *cookies2 = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
// for (NSHTTPCookie *c in cookies2)
// {
// NSLog(@"$$$$$$$$$$$$$c = %@", c);
// }
NSString *cookieString = [EHLCookieUtils getCookieString];
NSLog(@"cookieString : %@", cookieString);
//從 NSHTTPCookieStorage 獲取想要Cookie,此種獲取方式是獲取的cookies中的
NSArray <NSHTTPCookie *>*cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:self.webUrlStr]];
NSLog(@"cookies = %@", cookies);
NSHTTPCookie *cookie = nil;
for (NSHTTPCookie *tempCookie in cookies)
{
NSLog(@"tempCookie = %@", tempCookie);
NSLog(@"cookieName = %@, cookieValue = %@", tempCookie.name, tempCookie.value);
cookie = tempCookie;
}
NSDictionary *requestDic = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
NSLog(@"requestDic = %@", requestDic);
NSString *cookie1 = [requestDic objectForKey:@"Cookie"];
NSLog(@"cookie1 = %@", cookie1);
NSDictionary *cookieDic = [NSDictionary dictionaryWithObject:cookie.value forKey:cookie.name];
NSLog(@"cookieDic = %@", cookieDic);
return cookieDic;
}
打印結(jié)果為:
2020-09-26 02:50:56.894266+0800 葫蘆[21040:325891] cookieString : SESSION=ZjIzMmE2ODMtMzY0My00MjEwLTljMmEtMGU1NzJkNWM2ZjFh; SESSION=ZjVhYzUyZWYtZGQ1NS00NDVhLTgyMDUtOGVkZGM2ZjZmOWYw
從而可以看出來划乖,用getCookieString方法獲取到了兩個Cookie贬养,其中有一個也是無效的Cookie。
上述就是本人對WKWebView傳遞Cookie的心得琴庵,希望能夠幫助到大家误算。