一、存儲Cookie
1. 登錄成功后裆馒,存儲Cookie到本地姊氓。因為下次重啟APP,不再走登錄接口的話喷好,Cookie會丟失翔横,所以需要先存本地。
存儲時注意梗搅,要判斷是當(dāng)前登錄系統(tǒng)對應(yīng)的域名或IP禾唁。因為我遇到,登錄接口成功后无切,還出現(xiàn)了友盟的Cookie的荡短,防止被其他域名或IP的Cookie給覆蓋。
// 存儲Cookie? ? ? ?
NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];?? ? ? ?? ? ? ?
NSURL *url = [NSURL URLWithString:QDCConfigSet.host];?? ? ? ?? ? ? ?
NSString *IP = url.host;?? ? ? ?? ? ? ?
for (NSHTTPCookie *tempCookie in cookies) {?? ? ? ? ? ?? ? ? ? ? ?
????if ([tempCookie.domain isEqualToString:IP]) {?? ? ? ? ? ? ? ?? ? ? ? ? ? ? ?
????????[QDCUserDefaults setObject:tempCookie.properties forKey:@"KMobileServiceCookie"];? ? ? ? ? ?
????}? ? ? ?
}
二哆键、設(shè)置同步Cookie
方式1. iOS 11及以上的設(shè)置同步Cookie的方式:
(該方式有坑:說的是iOS11及以上就能用掘托,但是實際使用時,我遇到 iOS14一下幾率性的同步Cookie失敗的問題籍嘹,至今未找到原因闪盔,請知道的大佬指教,感恩有你X汀)
取本地存儲的Cookie的值锭沟,賦值到Cookie,然后針對目標域名或IP進行設(shè)置同步识补。
// 設(shè)置Cookie
- (void)setWebViewCookie {?? ???
????[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];? ?
????// 獲取cookie信息? ?
????NSDictionary *properties = [[NSUserDefaults standardUserDefaults] objectForKey:@"KMobileServiceCookie"];?? ?? ?
????NSMutableDictionary *muProperties = [NSMutableDictionary dictionaryWithDictionary:properties];? ??
????NSURL *url = [NSURL URLWithString:QDCConfigSet.managerHost];?? ?? ?
????NSString *IP = url.host;?? ?? ?
????[muProperties setValue:IP forKey:@"Domain"];?? ?? ?
????NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:muProperties];?? ?? ? ????[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];?? ?? ?
????// 同步cookie? ? WKHTTPCookieStore *cookieStrore = self.webView.configuration.websiteDataStore.httpCookieStore;?? ?? ?
????NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];?? ?? ?
????for (NSHTTPCookie *cookie in cookies) {?? ? ? ?? ? ? ?
????????if ([cookie.domain isEqualToString:IP]) {?? ? ? ? ? ?? ? ? ? ? ?
????????????[cookieStrore setCookie:cookie completionHandler:nil];? ? ? ?
????????}? ?
????}
}
方式2. 推薦使用族淮,JS注入的方式:
// 設(shè)置webView? ?
WKWebViewConfiguration *webConfig = [[WKWebViewConfiguration alloc] init];? ?
// 設(shè)置偏好設(shè)置? ?
webConfig.preferences = [[WKPreferences alloc] init];? ?
// 默認認為YES? ?
webConfig.preferences.javaScriptEnabled = YES;? ?
// web內(nèi)容處理池? ?
webConfig.processPool = [[WKProcessPool alloc] init];?? ?? ?
// 加cookie給h5識別,表明在iOS端打開該地址? ?
WKUserContentController *userContentController = WKUserContentController.new;? ?
WKUserScript *cookieScript = [self injectCookieScript];? ?
[userContentController addUserScript:cookieScript];? ?
webConfig.userContentController = userContentController;?? ?? ?
DWKWebView *webView = [[DWKWebView alloc] initWithFrame:self.view.bounds configuration:webConfig];
// 通過注入JS解決Cookies不同步的問題
- (WKUserScript *)injectCookieScript {?? ?? ?
????WKUserScript *userScript = [[WKUserScript alloc] initWithSource:[self cookieString] injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];?? ?? ?
????return userScript;
}
// 拼接cookie字符串
- (NSString *)cookieString {?? ?? ?
????[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];?? ?
????// 獲取cookie信息? ? ? ?
????NSDictionary *properties = [[NSUserDefaults standardUserDefaults] objectForKey:@"KMobileServiceCookie"];? ? ? ? ?
????NSMutableDictionary *muProperties = [NSMutableDictionary dictionaryWithDictionary:properties];? ? ? ? NSURL *url = [NSURL URLWithString:QDCConfigSet.managerHost];? ? ? ? ?
????NSString *IP = url.host;? ? ??? ?
????[muProperties setValue:IP forKey:@"Domain"];? ? ??? ?
????NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:muProperties];? ? ??? ? ????[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];?? ?? ?
????NSMutableString *script = [NSMutableString string];? ? [script appendString:@"var cookieNames = document.cookie.split('; ').map(function(cookie) { return cookie.split('=')[0] } );\n"];?? ?? ?
????for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {?? ? ? ?? ? ? ? if ([cookie.value rangeOfString:@"'"].location != NSNotFound) {? ? ? ? ? ?
????????????continue;? ? ? ?
? ? ? ? ?}? ? ? ? ? ? ? ?
????????if ([cookie.domain isEqualToString:IP]) {?? ? ? ? ? ?? ? ? ? ? ?
????????????NSString *string = [NSString stringWithFormat:@"%@=%@;domain=%@;path=%@",? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cookie.name, cookie.value, cookie.domain, cookie.path ?: @"/"];? ? ? ? ? ? ? ? ? ? ? ?
????????????[script appendFormat:@"if (cookieNames.indexOf('%@') == -1) {document.cookie='%@'; };\n", cookie.name, string];? ? ? ?
????????}? ? ? ? ? ?
????}? ?
? ? return script;
}