iOS 和 H5 页面交互(WKWebview 和 UIWebview cookie 设置)
- 作者: 游走的流浪汉
- 来源: 51数据库
- 2021-08-13
ios 和 h5 页面交互(wkwebview 和 uiwebview cookie 设置)
主要记录关于cookie相关的坑
1. uiwebview
1. uiwebview 相对比较简单 直接通过 nshttpcookiestorage 设置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];
```
2. 如果在第一次请求的时候需要在httprequest 通过setvalueforkey设置 headervalue
2. wkwebview
在使用wkwebview的时候也是需要分两种情况传递:
- 1.httprequest 请求url的时候携带 如后端php获取 cookie
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];
}
2.wkwebview 有跨域问题
* 最后要说的是以上方法如果 考虑跨域问题的话,uiwebview 是不会出现的,但是wkwebview是不允许跨域的,这个也是苹果考虑到安全性的方面,但是是可以处理的,目前我们的方案是以下两种
1.前端通过获取到cookie后 重新种植一下cookie ,通过 .xxx.com 模糊种植
2.让后端来处理,可以把用户相关信息如 uid传递给前端。
推荐阅读
