一、存储Cookie
1. 登录成功后,存储Cookie到本地。因为下次重启APP,不再走登录接口的话,Cookie会丢失,所以需要先存本地。
存储时注意,要判断是当前登录系统对应的域名或IP。因为我遇到,登录接口成功后,还出现了友盟的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"];? ? ? ? ? ?
????}? ? ? ?
}
二、设置同步Cookie
方式1. iOS 11及以上的设置同步Cookie的方式:
(该方式有坑:说的是iOS11及以上就能用,但是实际使用时,我遇到 iOS14一下几率性的同步Cookie失败的问题,至今未找到原因,请知道的大佬指教,感恩有你!)
取本地存储的Cookie的值,赋值到Cookie,然后针对目标域名或IP进行设置同步。
// 设置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注入的方式:
// 设置webView? ?
WKWebViewConfiguration *webConfig = [[WKWebViewConfiguration alloc] init];? ?
// 设置偏好设置? ?
webConfig.preferences = [[WKPreferences alloc] init];? ?
// 默认认为YES? ?
webConfig.preferences.javaScriptEnabled = YES;? ?
// web内容处理池? ?
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;
}