iOS 和 H5 页面交互(WKWebview 和 UIWebview cookie 设置)
程序员文章站
2022-11-06 18:32:44
iOS 和 H5 页面交互(WKWebview 和 UIWebview cookie 设置) 主要记录关于cookie相关的坑 1. UIWebview 1. UIWebview 相对比较简单 直接通过 NSHTTPCookieStorage 设置cookie就能实现。 代码部分 2. 如果在第一次 ......
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]; }