使用WKWebview接入淘宝二次授权免登录的坑
作者:互联网
1.阿里百川授权完成之后,进行淘宝授权免密登录时,时而需要密码登录,时而不需要(我当时用的WKWebview)
2.分析:当时我查阅了淘宝上的论坛和各大网站,都没有发现有人提到该问题,最终还是无果,我就开始看这个js源码,后面我就换了UIWebview,这个问题就没有,真是奇了怪了,怎么搞这个WKWebview都需要密码登录,当时我都怀疑苹果的WKWebview有bug;查了资料WKWebview和UIWebview区别才发现原来WKWebview不共享cookie
3.解决方法当然是加入cookie啦
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.url]];
NSMutableDictionary *cookieDic = [NSMutableDictionary dictionary];
NSMutableString *cookieValue = [NSMutableString stringWithFormat:@""];
NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *cookie in [cookieJar cookies]) {
[cookieDic setObject:cookie.value forKey:cookie.name];
}
// cookie重复,先放到字典进行去重,再进行拼接
for (NSString *key in cookieDic) {
NSString *appendString = [NSString stringWithFormat:@"%@=%@;", key, [cookieDic valueForKey:key]];
[cookieValue appendString:appendString];
}
[request addValue:cookieValue forHTTPHeaderField:@"Cookie"];
[webview loadRequest:request];
4.这个cookie还区分WKWebview的WKHTTPCookieStore和(UIWebView ,网络请求的NSHTTPCookie),其他cookie问题还得针对WKHTTPCookieStore进行入手解决,
标签:cookieValue,接入,淘宝,NSString,cookie,key,cookieDic,WKWebview 来源: https://blog.csdn.net/wanmeizty/article/details/93083751