1.UIWebView 注入Cookie
- (void)setCookieForPdfViewer {
NSString *cookieUser = [BDAccountManager sharedManager].loginInfoModel.cookieUser;
NSString *domain = [BDCommonInfoManager sharedManager].commonInfo.pdfDomain;
NSString *host = [BDCommonInfoManager sharedManager].commonInfo.pdfHost;
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSMutableDictionary *cookieProperties1 = [NSMutableDictionary dictionary];
[cookieProperties1 setObject:@"cookie_user" forKey:NSHTTPCookieName];
[cookieProperties1 setObject:cookieUser forKey:NSHTTPCookieValue];
[cookieProperties1 setObject:domain forKey:NSHTTPCookieDomain];
[cookieProperties1 setObject:@"/" forKey:NSHTTPCookiePath];
[cookieProperties1 setObject:host forKey:NSHTTPCookieOriginURL];
NSHTTPCookie *cookie1 = [NSHTTPCookie cookieWithProperties:cookieProperties1];
NSMutableDictionary *cookieProperties2 = [NSMutableDictionary dictionary];
[cookieProperties2 setObject:@"xxxx.id" forKey:NSHTTPCookieName];
[cookieProperties2 setObject:@"0" forKey:NSHTTPCookieValue];
[cookieProperties2 setObject:domain forKey:NSHTTPCookieDomain];
[cookieProperties2 setObject:@"/" forKey:NSHTTPCookiePath];
[cookieProperties2 setObject:host forKey:NSHTTPCookieOriginURL];
NSHTTPCookie *cookie2 = [NSHTTPCookie cookieWithProperties:cookieProperties2];
[cookieStorage setCookie:cookie1];
[cookieStorage setCookie:cookie2];
}
- (void)dealloc {
//NSHTTPCookieStorage 是全局的,出于安全性考虑,清除所有cookie
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSString *host = [BDCommonInfoManager sharedManager].commonInfo.pdfHost;
NSArray *cookies = [cookieStorage cookiesForURL:[NSURL URLWithString:host]];
for (NSHTTPCookie *cookie in cookies) {
[cookieStorage deleteCookie:cookie];
}
}
由于使用UIWebView 会导致内存暴涨,即使从web页面退出后,内存也没有恢复,如果返回进出UIWebView的话甚至会涨到250M以上
因此改用WKWebView
2.WKWebView注入Cookie
准备需要注入的数据
- (NSArray<NSHTTPCookie *> *)pdfViewCookies {
if (!_pdfViewCookies) {
NSString *cookieUser = [BDAccountManager sharedManager].loginInfoModel.cookieUser;
NSString *domain = [BDCommonInfoManager sharedManager].commonInfo.pdfDomain;
NSString *host = [BDCommonInfoManager sharedManager].commonInfo.pdfHost;
NSMutableDictionary *cookieProperties1 = [NSMutableDictionary dictionary];
[cookieProperties1 setObject:@"cookie_user" forKey:NSHTTPCookieName];
[cookieProperties1 setObject:cookieUser forKey:NSHTTPCookieValue];
[cookieProperties1 setObject:domain forKey:NSHTTPCookieDomain];
[cookieProperties1 setObject:@"/" forKey:NSHTTPCookiePath];
[cookieProperties1 setObject:host forKey:NSHTTPCookieOriginURL];
NSHTTPCookie *cookie1 = [NSHTTPCookie cookieWithProperties:cookieProperties1];
NSMutableDictionary *cookieProperties2 = [NSMutableDictionary dictionary];
[cookieProperties2 setObject:@"xxxx.id" forKey:NSHTTPCookieName];
[cookieProperties2 setObject:@"0" forKey:NSHTTPCookieValue];
[cookieProperties2 setObject:domain forKey:NSHTTPCookieDomain];
[cookieProperties2 setObject:@"/" forKey:NSHTTPCookiePath];
[cookieProperties2 setObject:host forKey:NSHTTPCookieOriginURL];
NSHTTPCookie *cookie2 = [NSHTTPCookie cookieWithProperties:cookieProperties2];
_pdfViewCookies = @[cookie1, cookie2];
}
return _pdfViewCookies;
}
- (NSArray <WKUserScript *>*)cookieScripts {
NSMutableArray *cookieScripts = [NSMutableArray array];
for (NSHTTPCookie*cookie in self.pdfViewCookies) {
NSString *cookieString = [NSString stringWithFormat:@"document.cookie = '%@=%@';",cookie.name,cookie.value];
WKUserScript * cookieScript = [[WKUserScript alloc] initWithSource: cookieString injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
[cookieScripts addObject:cookieScript];
}
return cookieScripts;
}
方式一:
- (void)setupViews{
WKWebView * webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:[self cookiConfig]];
self.webView = webView;
webView.navigationDelegate = self;
webView.UIDelegate = self;
[self.view addSubview:self.webView];
}
/**
1.通过WKWebView的config设置Cookie
*/
- (WKWebViewConfiguration *)cookiConfig {
WKWebViewConfiguration* webViewConfig = [[WKWebViewConfiguration alloc]init];
WKUserContentController* userContentController = [[WKUserContentController alloc]init];
// 1.添加cookie
for (WKUserScript *script in [self cookieScripts]) {
[userContentController addUserScript:script];
}
return webViewConfig;
}
方式二:
/**
2.通过LoadRequest 方式添加Cookie
eg:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[self.urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
[request addValue:[self readCurrentCookie] forHTTPHeaderField:@"Cookie"];
[self.webView loadRequest:request];
*/
- (NSString*)readCurrentCookie{
NSMutableString *cookieString = [[NSMutableString alloc] init];
for (NSHTTPCookie*cookie in self.pdfViewCookies) {
[cookieString appendFormat:@"'%@=%@';",cookie.name,cookie.value];
}
//删除最后一个“;”
[cookieString deleteCharactersInRange:NSMakeRange(cookieString.length - 1, 1)];
return cookieString;
}
- (void)loadRequest {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[self.urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
[request setValue:[self readCurrentCookie] forHTTPHeaderField:@"Cookie"];
[self.webView loadRequest:request];
}
方式三:
/**
* 3. webView:(WKWebView *)webView didFinishNavigation 中通过JS 注入cookie
* 解决Cookie丢失问题
*/
- (void)injectCookieByJsFunction {
//js函数
NSString *JSFuncString =
@"function setCookie(name,value,expires)\
{\
var oDate=new Date();\
oDate.setDate(oDate.getDate()+expires);\
document.cookie=name+'='+value+';expires='+oDate+';path=/'\
}\
function getCookie(name)\
{\
var arr = document.cookie.match(new RegExp('(^| )'+name+'=({FNXX==XXFN}*)(;|$)'));\
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 self.pdfViewCookies) {
NSString *excuteJSString = [NSString stringWithFormat:@"setCookie('%@', '%@', 1);", cookie.name, cookie.value];
[JSCookieString appendString:excuteJSString];
}
//执行js
[self.webView evaluateJavaScript:JSCookieString completionHandler:nil];
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
// 通过js 方法注入cookie
[self injectCookieByJsFunction];
}