标签:RKE 通过 art 问题 lse cts pre rip remove
主要记录关于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的时候也是需要分两种情况传递:
2.注入js 目的是让前端从页面里边获取到cookie 可以通过在document.cookie 设置 通过WKWebview 初始化时候把js传递过去
`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];
}
iOS 和 H5 页面交互(WKWebview 和 UIWebview cookie 设置)
标签:RKE 通过 art 问题 lse cts pre rip remove
原文地址:https://www.cnblogs.com/keyan1102/p/11729795.html