一般在web与js的交互中想再post时需要拿到web里的用户信息,登录状态等,需要传递web的cooki
wk设置-->wk获取-->存储-->af取-->af设置
wkwebview的处理
WKWebViewConfiguration *config = [WKWebViewConfiguration new];
// 设置偏好设置
config.preferences = [WKPreferences new];
// 默认为0
config.preferences.minimumFontSize = 10;
// 默认为yes
config.preferences.javaScriptEnabled = YES;
// 在iOS上默认为NO,表示不能自动通过窗口打开
config.preferences.javaScriptCanOpenWindowsAutomatically = NO;
// web内容处理池
config.processPool = [[WKProcessPool alloc] init];
// 注入JS对象名称AppModel,当JS通过AppModel来调用时,
// 我们可以在WKScriptMessageHandler代理中接收到
userContentController = [[WKUserContentController alloc]init];
config.userContentController = userContentController;
[userContentController addScriptMessageHandler:self name:@"AppModel"];
WKWebView *wkWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) configuration:config];
wkWebView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
wkWebView.backgroundColor = [UIColor whiteColor];
wkWebView.navigationDelegate = self;
wkWebView.UIDelegate = self;
(这里是添加cookie(重点代码))
WKUserScript * cookieScript = [[WKUserScript alloc] initWithSource: @"document.cookie ='TeskWKCookieKey1=TeskWKCookieValue1';document.cookie = 'TeskWKCookieKey2=TeskWKCookieValue2';"injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
[userContentController addUserScript:cookieScript];
NSMutableURLRequest *request= [NSMutableURLRequest requestWithURL:[NSURL URLWithString:baseUrl]];
[request setValue:[NSString stringWithFormat:@"%@=%@",@"testWKcookie", @"testWKcookievalue"] forHTTPHeaderField:@"Cookie"];
(把我想象成一条分割线呀~~~)
[wkWebView loadRequest:request];
self.wKWebView = wkWebView;
Delegate(重点代码)
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
NSHTTPURLResponse *response = (NSHTTPURLResponse *)navigationResponse.response;
NSArray *cookies =[NSHTTPCookie cookiesWithResponseHeaderFields:[response allHeaderFields] forURL:response.URL];
/////////////// 存cookie(主要是这里)
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:cookies];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:@"WebViewCookie"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"存:cookies : %@",cookies);
(把我想象成一条分割线呀~~~)
//读取wkwebview中的cookie 方法1
for (NSHTTPCookie *cookie in cookies) {
// [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
// NSLog(@"webview ---- %@", cookie);
}
//读取wkwebview中的cookie 方法2 读取Set-Cookie字段
// NSString *cookieString = [[response allHeaderFields] valueForKey:@"Set-Cookie"];
// NSLog(@"wkwebview中的cookie:%@", cookieString);
//看看存入到了NSHTTPCookieStorage了没有
NSHTTPCookieStorage *cookieJar2 = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *cookie in cookieJar2.cookies) {
// NSLog(@"NSHTTPCookieStorage中的cookie%@", cookie);
// NSLog(@"webview ---- %@", cookie);
}
decisionHandler(WKNavigationResponsePolicyAllow);
}
Afnetworking处的处理
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager setSecurityPolicy:securityPolicy];
[manager.requestSerializer setHTTPShouldHandleCookies:NO];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//////////////设置web的cookie给af(重点代码)
NSData *cookiesdata = [[NSUserDefaults standardUserDefaults] objectForKey:@"WebViewCookie"];
if([cookiesdata length]) {
NSArray *cookies = [NSKeyedUnarchiver unarchiveObjectWithData:cookiesdata];
NSHTTPCookie *cookie;
for (cookie in cookies) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
NSLog(@"取:cookies : %@",cookies);
}
}
(把我想象成一条分割线呀~~~)
[manager POST:url parameters:body progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (success) {
success(task, responseObject);
NSDictionary *fields = ((NSHTTPURLResponse*)task.response).allHeaderFields;
// NSLog(@"post --- fields = %@",[fields description]);
// NSURL *url = [NSURL URLWithString:@"http://dev.skyfox.org/cookie.php"];
//获取cookie方法1
NSArray *cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:fields forURL:[NSURL URLWithString:url]];
for (NSHTTPCookie *cookie in cookies) {
// NSLog(@"post --- cookie,name:= %@,valuie = %@",cookie.name,cookie.value);
}
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(task, error);
}
}]