IOS与H5交互,cookie传值

在iPhone系统升级前:10.1之前的时候我们在与H5交互直接通过传递cookie就可以实现H5的部分参数需求。


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值了!

你可能感兴趣的:(IOS与H5交互,cookie传值)