前言
首先這篇文章所描述的僅僅只是cookie中的Session字段的問(wèn)題贡蓖。
我理解的Session 對(duì)象是存儲(chǔ)特定的用戶(hù)會(huì)話(huà)所需的配置信息券躁。當(dāng)用戶(hù)在Web頁(yè)面跳轉(zhuǎn)的時(shí)候申鱼,Session不會(huì)丟失风钻,會(huì)一直存在下去。用戶(hù)請(qǐng)求到Web頁(yè)面時(shí)挽放,如果用戶(hù)還沒(méi)有會(huì)話(huà)绍赛,則服務(wù)器將自動(dòng)創(chuàng)建一個(gè),它將被存儲(chǔ)于cookie中辑畦。
我開(kāi)發(fā)中所遇到的問(wèn)題是吗蚌,某些H5業(yè)務(wù)需要驗(yàn)證登錄后才可以使用,并且還要帶一個(gè)特殊的cookie字段表示來(lái)源纯出。這就要求Session在WebView中可以一直保持著蚯妇,否則沒(méi)有Session敷燎,服務(wù)器每次都需要在驗(yàn)證一次用戶(hù)信息。
WKWebView
最開(kāi)始我使用WKWebView箩言,畢竟效率高了很多硬贯,但是在cookie這塊確實(shí)是很坑。
Cookie并不會(huì)再加載后自動(dòng)保存到NSHTTPCookieStorage中陨收,也就是說(shuō)饭豹,發(fā)起請(qǐng)求的時(shí)候不會(huì)自動(dòng)帶上存儲(chǔ)于NSHTTPCookieStorage容器中的Cookie,內(nèi)存根本不用UIWebView那套了务漩≈羲ィ可能是有自己的私有存儲(chǔ)吧(不太清楚)。
在這里看到饵骨,可以使用同一個(gè)的WKProcessPool實(shí)例來(lái)使不同的WKWebView擁有相同的Cookie翘悉。
/**
構(gòu)建一個(gè)單例用來(lái)保存WKProcessPool
*/
+ (instancetype)shareInstance{
static WKProcesspoolManager *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[WKProcesspoolManager alloc] init];
});
return instance;
}
- (instancetype)init {
self.wkProcessPool = [[WKProcessPool alloc] init];
}
我經(jīng)過(guò)這樣嘗試后發(fā)現(xiàn),還是不能解決不同WKWebView中session保持的問(wèn)題居触,于是我就想看看在WKWebView中的Cookie到底保存了什么字段妖混。
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
NSHTTPURLResponse * response = (NSHTTPURLResponse *)navigationResponse.response;
NSArray *cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:response.allHeaderFields forURL:[NSURL URLWithString:_urlStr]];
for (NSHTTPCookie *cookie in cookies) {
NSLog(@"cookie = %@",cookie);
}
decisionHandler(WKNavigationResponsePolicyAllow);
}
拿到Cookie后發(fā)現(xiàn),里面竟然只能獲取到我自己在請(qǐng)求時(shí)設(shè)置的字段饼煞。其他的根本就沒(méi)有源葫。而Sessionid這個(gè)Cookie 可能是被服務(wù)器設(shè)置了HttpOnly屬性了,WKWebView將它保護(hù)起來(lái)诗越。所以想要修改它砖瞧,JS是不可能的。只能通過(guò)WKWebView的接口入手了嚷狞,可是我并沒(méi)有發(fā)現(xiàn)這樣的接口块促。所以我只能換回UIWebView了。
UIWebView
換回來(lái)之后床未,其實(shí)一切都很簡(jiǎn)單了竭翠。
UIWebView中的請(qǐng)求之后Cookie都保存在NSHTTPCookieStorage中了,并且sessionid也存在薇搁。
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSHTTPCookieStorage *myCookie = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *cookie in [myCookie cookies]) {
NSLog(@"%@", cookie);
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
}
}
這樣可以將完整的Cookie保存下來(lái)斋扰,以便自動(dòng)登錄。接下來(lái)就是將保存起來(lái)的cookis設(shè)置在request中就可以了
NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL
URLWithString:HOST]]; // HOST就是你web服務(wù)器的域名地址
// 設(shè)置header
for (NSHTTPCookie *cookie in cookies){
// cookiesWithResponseHeaderFields方法啃洋,需要為URL設(shè)置一個(gè)cookie為NSDictionary類(lèi)型的header传货,注意NSDictionary里面的forKey需要是@"Set-Cookie"
NSString *str = [[NSString alloc] initWithFormat:@"%@=%@",[cookie name],[cookie value]];
NSDictionary *dic = [NSDictionary dictionaryWithObject:str forKey:@"Set-Cookie"];
NSArray *headeringCookie = [NSHTTPCookie cookiesWithResponseHeaderFields:dic forURL:[NSURL URLWithString:HOST]];
// 設(shè)置Cookie,只要訪(fǎng)問(wèn)URL為HOST的網(wǎng)頁(yè)時(shí)宏娄,會(huì)自動(dòng)附帶上設(shè)置的header
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:headeringCookie
forURL:[NSURL URLWithString:HOST]
mainDocumentURL:nil];
}
這樣就可以在不同的UIWebView中共享同一個(gè)sessionid了问裕。
最后
在換回UIWebView后,我測(cè)試了一下孵坚。在第一個(gè)頁(yè)面設(shè)置sessionid然后第二個(gè)頁(yè)面讀取sessionid粮宛,是可以讀取到的窥淆。
然后我又使用了WKWebView測(cè)試發(fā)現(xiàn),還是獲取不到巍杈。
我又想到微信的WebView內(nèi)核好像全是WKWebView了忧饭,于是我在微信中打開(kāi)測(cè)試了一下,發(fā)現(xiàn)竟然可以獲取到到sessionid筷畦。
也就是說(shuō)微信做到了眷昆。果然還是有方法的。只是自己水平不足罷了汁咏。希望有大牛能提點(diǎn)我一下亚斋,也希望這篇文章給予有需要的人一點(diǎn)幫助。
2017-09-05 更新
-----------------------------------------------------------------------------------------------
上個(gè)月攘滩,全面使用了WKWebView了帅刊,也解決sessionid的問(wèn)題。
具體解決過(guò)程如下:
- 首先在登錄的時(shí)候漂问,在網(wǎng)絡(luò)請(qǐng)求結(jié)束的地方用一個(gè)單例來(lái)保存這次cookie赖瞒。大概代碼:
WKCookieSyncManager *cookiesManager = [WKCookieSyncManager sharedWKCookieSyncManager];
[cookiesManager setCookie];
@interface WKCookieSyncManager () <WKNavigationDelegate>
@property (nonatomic, strong) WKWebView *webView;
///用來(lái)測(cè)試的url這個(gè)url是不存在的
@property (nonatomic, strong) NSURL *testUrl;
@end
@implementation WKCookieSyncManager
+ (instancetype)sharedWKCookieSyncManager {
static WKCookieSyncManager *sharedWKCookieSyncManagerInstance = nil;
static dispatch_once_t predicate;
dispatch_once(&predicate, ^{
sharedWKCookieSyncManagerInstance = [[self alloc] init];
});
return sharedWKCookieSyncManagerInstance;
}
- (void)setCookie {
//判斷系統(tǒng)是否支持wkWebView
Class wkWebView = NSClassFromString(@"WKWebView");
if (!wkWebView) {
return;
}
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
config.processPool = self.processPool;
self.webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:config];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:self.testUrl];
self.webView.navigationDelegate = self;
[self.webView loadRequest:request];
}
#pragma - get
- (WKProcessPool *)processPool {
if (!_processPool) {
static dispatch_once_t predicate;
dispatch_once(&predicate, ^{
_processPool = [[WKProcessPool alloc] init];
});
}
return _processPool;
}
- (NSURL *)testUrl {
if (!_testUrl) {
NSURLComponents *urlComponents = [NSURLComponents new];
urlComponents.host = @"tm.lilanz.com";
urlComponents.scheme = @"http";
urlComponents.path = @"a.aspx";
// NSLog(@"測(cè)試url=%@", urlComponents.URL);
return urlComponents.URL;
}
return _testUrl;
}
#pragma mark - WKNavigationDelegate
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
//取出cookie
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *cookie in cookieStorage.cookies) {
NSLog(@"name = %@ value = %@",cookie.name,cookie.value);
}
//js函數(shù)
NSString *JSFuncString =
@"function setCookie(name,value,expires)\
{\
var oDate=new Date();\
oDate.setDate(oDate.getDate()+expires);\
document.cookie=name+'='+value+';expires='+oDate;\
}\
function getCookie(name)\
{\
var arr = document.cookie.match(new RegExp('(^| )'+name+'=([^;]*)(;|$)'));\
if(arr != null) return unescape(arr[2]); return null;\
}\
function delCookie(name)\
{\
var exp = new Date();\
exp.setTime(exp.getTime() - 1);\
var cval=getCookie(name);\
if(cval!=null) document.cookie= name + '='+cval+';expires='+exp.toGMTString();\
}";
//拼湊js字符串
NSMutableString *JSCookieString = JSFuncString.mutableCopy;
for (NSHTTPCookie *cookie in cookieStorage.cookies) {
NSString *excuteJSString = [NSString stringWithFormat:@"setCookie('%@', '%@', 1);", cookie.name, cookie.value];
[JSCookieString appendString:excuteJSString];
}
//執(zhí)行js
[webView evaluateJavaScript:JSCookieString completionHandler:nil];
}
注:或者在其它有網(wǎng)絡(luò)請(qǐng)求的地方,但是這個(gè)地方必須先執(zhí)行于你要使用的網(wǎng)頁(yè)之前蚤假。
然后在加載WKWebView的地方使用即可栏饮。
WKCookieSyncManager *cookiesManager = [WKCookieSyncManager sharedWKCookieSyncManager];
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
configuration.processPool = cookiesManager.processPool;
_wkWebView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:configuration];