主要記錄關於cookie相關的坑前端
``` NSURL *cookieHost = [NSURL URLWithString:self.domain]; // 設定 cookie NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties: [NSDictionary dictionaryWithObjectsAndKeys: [cookieHost host], NSHTTPCookieDomain, [cookieHost path], NSHTTPCookiePath, self.cookieKey, NSHTTPCookieName, self.cookieValue, NSHTTPCookieValue, nil]]; // 加入cookie [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie]; ```
在使用WKWebview的時候也是須要分兩種狀況傳遞:ios
2.注入js 目的是讓前端從頁面裏邊獲取到cookie 能夠經過在document.cookie 設置 經過WKWebview 初始化時候把js傳遞過去web
`WKUserScript * cookieScript = [[WKUserScript alloc] initWithSource: cookieValue injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
後端
3.NSHTTPCookieStorage 彷佛不攜帶沒問題,由於咱們目前沒有經過這個傳遞cookie跨域
WKWebview三個處理步驟: (1)iOS11,WKHTTPCookieStore 直接傳遞。(若是是隻支持iOS11,下面兩步能夠不作); (2)iOS8-iOS10, js注入; (3)PHP攜帶cookie方式安全
#pragma mark - WKWebview // iOS11 - (void)setWkCookie:(WKWebView *)wkWebview completionHandler:(nullable void (^)(void))comple { NSURL *cookieHost = [NSURL URLWithString:self.domain]; // 設定 cookie NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties: [NSDictionary dictionaryWithObjectsAndKeys: [cookieHost host], NSHTTPCookieDomain, [cookieHost path], NSHTTPCookiePath, self.cookieKey, NSHTTPCookieName, self.cookieValue, NSHTTPCookieValue, // [NSDate dateWithTimeIntervalSinceNow:30*60*60],NSHTTPCookieExpires, nil]]; // 加入cookie //發送請求前插入cookie; if (@available(iOS 11.0, *)) { WKHTTPCookieStore *cookieStore = wkWebview.configuration.websiteDataStore.httpCookieStore; [cookieStore setCookie:cookie completionHandler:^{ comple?comple():nil; }]; } else { } } // JS攜帶cookie的形式 - (void)setWkJsCookie:(WKUserContentController *)userContentController { // 單個cookie,多個的話,再加上document.cookie ='%@=%@';一次 NSString *cookieStr = [NSString stringWithFormat:@"document.cookie ='%@=%@';",self.cookieKey,self.cookieValue]; WKUserScript * cookieScript = [[WKUserScript alloc] initWithSource: cookieStr injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO]; [userContentController addUserScript:cookieScript]; } // PHP攜帶cookie的形式 - (void)setWkPHPCookie:(NSMutableURLRequest *)request { //經過host關聯cookie。 NSMutableDictionary *cookieDic = [NSMutableDictionary dictionary]; NSMutableString *cookieValue = [NSMutableString stringWithFormat:@""]; NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; for (NSHTTPCookie *cookie in [cookieStorage cookies]) { [cookieDic setObject:cookie.value forKey:cookie.name]; } if ([cookieDic objectForKey:[CookieManager shareInstance].cookieKey]) { [cookieDic removeObjectForKey:[CookieManager shareInstance].cookieKey]; } // cookie重複,先放到字典進行去重,再進行拼接 for (NSString *key in cookieDic) { NSString *appendString = [NSString stringWithFormat:@"%@=%@;", key, [cookieDic valueForKey:key]]; [cookieValue appendString:appendString]; } [cookieValue appendString:[NSString stringWithFormat:@"%@ = %@;",self.cookieKey,self.cookieValue]]; [request addValue:cookieValue forHTTPHeaderField:@"Cookie"]; }
#pragma mark - Webview // 客戶端添加cookie - (void)setWebCookie { NSURL *cookieHost = [NSURL URLWithString:self.domain]; // 設定 cookie NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties: [NSDictionary dictionaryWithObjectsAndKeys: [cookieHost host], NSHTTPCookieDomain, [cookieHost path], NSHTTPCookiePath, self.cookieKey, NSHTTPCookieName, self.cookieValue, NSHTTPCookieValue, nil]]; // 加入cookie [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie]; }