
NSURL *cookieHost = [NSURL URLWithString:urlString]
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary]
NSString *app_Version = [NSString stringWithFormat:@"%@",[infoDictionary objectForKey:@"CFBundleShortVersionString"]]
NSHTTPCookie *cookieClient = [NSHTTPCookie cookieWithProperties:[NSDictionary dictionaryWithObjectsAndKeys:[cookieHost host], NSHTTPCookieDomain,[cookieHost path], NSHTTPCookiePath,@"Client", NSHTTPCookieName, @"ios", NSHTTPCookieValue,nil]]
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookieClient]
然后,从系统升级后,10.2起,cookie传值已经无法正常传递过去。即H5无法收到ios平台发送过去的cookie参数。
究其原因:IOS 10.2以后不再支持http请求协议,称为支持的https协议,所以http的协议无法通过cookie传递到H5页面。
解决方法:
新增一个字段:key:httpOnly value:false。
NSHTTPCookie *cookieClient2 = [NSHTTPCookie cookieWithProperties:[NSDictionary dictionaryWithObjectsAndKeys:[cookieHost host], NSHTTPCookieDomain,[cookieHost path], NSHTTPCookiePath,@"httpOnly", NSHTTPCookieName, @"false", NSHTTPCookieValue,nil]]
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookieClient]
这时候再次运行,那么H5已经可以接收到我们的cookie值了!
1.定义一个方法,方法名setTheCookieOfWebView,方法具体实现如下:
2.在wkwebview的代理中添加上面写的方法及可。
/* 开始返回内容 */
-- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
[self setTheCookieOfWebView]
NSLog(@"开始返回内容")
}
ps:wkwebview的坑还是比较多的,特别是cookie的问题,每次请求的时候不会自动带,所以采用了另外一种思路,直接取本地的cookie注入js的document,也解决了web有时候取不到cookie的问题。暂时没发现什么问题,如有问题欢迎留言讨论。
项目接近尾声了,wkwebview的封装也差不多了,等有时间整理下把js和web的交互写下。
1.利用cookie对象Cookie是服务器保存在客户端中的一小段数据信息。使用Cookie有一个前提,就是客户端浏览器允许使用Cookie并对此做出相应的设置。一般不赞成使用Cookie。
(1)后台代码
Cookie cookie=new Cookie("name", "hello")
response.addCookie(cookie)
(2)前台代码
Cookie[] cookies=request.getCookies()
for(int i=0i<cookies.lengthi++){
if(cookies[i].getName().toString().equals("name")){
out.print(cookies[i].getValue())
}
}
2.利用session对象
session对象表示特定会话session的用户数据。客户第一次访问支持session的JSP网页,服务器会创建一个session对象记录客户的信息。当客户访问同一网站的不同网页时,仍处于同一个session中。
(1)后台代码
request.getSession().setAttribute("name", name)
request.getSession().setMaxInactiveInterval(2)
response.sendRedirect("welcome.jsp")
(2)前台代码(jsp页面)
Object user=request.getSession().getAttribute("name")
3.利用request重定向,设置setAttribute
(1)后台代码
request.setAttribute("name", "cute")
request.getRequestDispatcher("welcome.jsp").forward(request, response) //网址不会改变
PS:如果后台使用的转发代码为 response.sendRedirect("welcome.jsp") //网址变为welcome.jsp
则request设置的参数无效,因为已经切换到另一个请求了,request参数的有效期为本次请求。
(2)前台代码
String name=request.getAttribute("name").toString()
4.利用Ajax进行异步数据请求(得到的数据可以以json或xml格式返回,便于处理)
(1)后台代码案例(运用servlet传输数据)
public class TestServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public TestServlet() {
super()
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response)
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html")
PrintWriter out = response.getWriter()
String data="[{\"name\":\"apple\",\"price\":23},{\"name\":\"banana\",\"price\":12},{\"name\":\"orange\",\"price\":8}]"
out.write(data)
out.flush()
out.close()
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
2.前台js请求处理数据代码
function createXMLHttpRequest(){
var xmlrequest
if(window.XMLHttpRequest){
xmlrequest=new XMLHttpRequest()
}else if(window.ActiveXObject){
try{
xmlrequest=new ActiveXObject("Msxm12.XMLHTTP")
}catch(e){
try{
xmlrequest=new ActiveXObject("Microsoft.XMLHTTP")
}catch(e){
xmlrequest=""
}
}
}
return xmlrequest
}
//获取数据的函数
function change(){
var xmlrequest=createXMLHttpRequest()
xmlrequest.open("POST","TestServlet",true)
xmlrequest.onreadystatechange=function(){
if(xmlrequest.readyState==4&&xmlrequest.status==200){
var data=JSON.parse(xmlrequest.responseText)
var content="<table border=1>"
for(var i=0i<data.lengthi++){
content+="<tr>"
for(o in data[i]){
content+="<td>"+data[i][o]+"</td>"
}
content+="</tr>"
}
content+="</table>"
document.getElementById("test").innerHTML=content
}
}
xmlrequest.send()
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)