新浦京81707con > 首页 > iOS的Cookie存取看我绝对够

原标题:iOS的Cookie存取看我绝对够

浏览次数:65 时间:2020-03-22

近期某些厂商为了快速出一款app,相当多时候利用UINavigationController WebView恐怕NavigationController UITabbarVC WebView的方法,那样就不可防止的供给使用cookie与Html5并行,上边陈诉三种常用情景下cookie的丰裕方法:

眼下某些商厦为了急忙出一款app,非常多时候使用UINavigationController WebView或许NavigationController UITabbarVC WebView的不二等秘书诀,这样就不可防止的必要运用cookie与HTML5人机联作,上面叙述两种常用情景下cookie的丰硕方法:

一. UIWebView:

// 工厂类中存储cookie的方法  saveCookies { // 创建一个可变字典存放cookie NSMutableDictionary *fromappDict = [NSMutableDictionary dictionary]; [fromappDict setObject:@"fromapp" forKey:NSHTTPCookieName]; [fromappDict setObject:@"ios" forKey:NSHTTPCookieValue]; // kDomain是公司app网址 [fromappDict setObject:kDomain forKey:NSHTTPCookieDomain]; [fromappDict setObject:kDomain forKey:NSHTTPCookieOriginURL]; [fromappDict setObject:@"/" forKey:NSHTTPCookiePath]; [fromappDict setObject:@"0" forKey:NSHTTPCookieVersion]; // 将可变字典转化为cookie NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:fromappDict]; // 获取cookieStorage NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage] // 存储cookie [cookieStorage setCookie:cookie];}
  • UIWebView使用时间较长,只要在cookieStorage中装置了相应的cookie,每一趟就会自行带上;
  • 可是这么的弊病是随着与H5的互相扩张,Cookie占用的空中越来越大,每便人机联作都夹带多量的cookie,不唯有平添服务器端压力,也浪费用户的流量。比如每一趟人机联作都夹带5kb的cookie内容,不过的确使用的唯有两七百字节。

一. UIWebView:

二. WKWebView

  • WKWebView相比于UIWebView:

    • 进程快了一倍,内部存款和储蓄器却减弱为本来的四分之二;
    • cookie不再是自动引导,须要手动设置;
    • 相互作用尤其一箭穿心,举个例子app尾部八个tabBar也都以网页的,在UIWebView下点击,整个H5页面都会闪白一下,不过在WKWebView下点击,几个tabBar效果与原生app效果尤其相通,不会有闪白现象。
    • 增减了一些代理方法,更实惠的进行磋商拦截和进程条显示
  • 1.在创制的时候存放到WKUserScript中开展增多cookie

 WKWebViewConfiguration *webConfig = [[WKWebViewConfiguration alloc] init]; // 设置偏好设置 webConfig.preferences = [[WKPreferences alloc] init]; // 默认为0 webConfig.preferences.minimumFontSize = 10; // 默认认为YES webConfig.preferences.javaScriptEnabled = YES; // 在iOS上默认为NO,表示不能自动通过窗口打开 webConfig.preferences.javaScriptCanOpenWindowsAutomatically = NO; // web内容处理池 webConfig.processPool = [[WKProcessPool alloc] init]; // 将所有cookie以document.cookie = 'key=value';形式进行拼接 #warning 然而这里的单引号一定要注意是英文的,不要问我为什么告诉你这个 NSString *cookieValue = @"document.cookie = 'fromapp=ios';document.cookie = 'channel=appstore';"; // 加cookie给h5识别,表明在ios端打开该地址 WKUserContentController* userContentController = WKUserContentController.new; WKUserScript * cookieScript = [[WKUserScript alloc] initWithSource: cookieValue injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO]; [userContentController addUserScript:cookieScript]; webConfig.userContentController = userContentController; WKWebView *wkWebView = [[WKWebView alloc] initWithFrame:frame configuration:webConfig]; wkWebView.UIDelegate = wkWebView; wkWebView.navigationDelegate = wkWebView;
  • 2.加载某些url的时候增加cookie

一旦WKWebView在加载url的时候须求增加cookie,须求先手动获取当前NSHTTPCookieStorage中的全部cookie,然后将cookie放到NSMutableU奥迪Q3LRequest诉求头中

- loadRequestWithUrlString:(NSString *)urlString { // 在此处获取返回的cookie 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]; } NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]]; [request addValue:cookieValue forHTTPHeaderField:@"Cookie"]; [self loadRequest:request];}

// 工厂类中蕴藏cookie的不二秘诀

3.AFNetworking

AFNetworking存取cookie就相比遍布了,话非常少说,都在代码里了

 // 获取AFHTTPRequestOperationManager AFHTTPRequestOperationManager *operationManager = [AFHTTPRequestOperationManager manager]; // 创建可变字典用于存放Cookie NSMutableDictionary *cookieDic = [NSMutableDictionary dictionary]; // 存放新添加的cookie #warning 单个的Cookie还好,但是楼主在工厂类方法中拼接了一个包含6个设备信息的value值,最后忘记加分号了,测试也没测出来 #warning 因为不影响功能,但是后面的cookie就自动拼接上了,H5那边也识别不到,险造成重大事故,望后来者看到,提前做好准备(就凭这个坑,你得给我个赞) NSMutableString *cookieValue = [NSMutableString stringWithFormat:@"fromapp=ios;"]; // 获取 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]; } // 将cookie存到请求头中 [operationManager.requestSerializer setValue:cookieValue forHTTPHeaderField:@"Cookie"]; // 拼接url地址 NSString *urlStr = [NSString stringWithFormat:@"%@%@", kHostIP, kPath]; // 设置参数字典 NSDictionary *paraDict = @{ @"key" : value }; // 发送请求,处理结果 [operationManager POST:urlStr parameters:paraDict success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) { NSLog(@"responseObject-->%@", responseObject); } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) { NSLog(@"error-->%@", error); }];

(void)saveCookies {

// 成立三个可变字典寄存cookie

NSMutableDictionary *fromappDict = [NSMutableDictionary dictionary];

[fromappDict setObject:@"fromapp" forKey:NSHTTPCookieName];

[fromappDict setObject:@"ios" forKey:NSHTTPCookieValue];

// kDomain是公司app网址

[fromappDict setObject:kDomain forKey:NSHTTPCookieDomain];

[fromappDict setObject:kDomain forKey:NSHTTPCookieOriginURL];

[fromappDict setObject:@"/" forKey:NSHTTPCookiePath];

[fromappDict setObject:@"0" forKey:NSHTTPCookieVersion];

// 将可变辞典转变为cookie

NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:params];

// 获取cookieStorage

NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage]

// 存储cookie

[cookieStorage setCookie:cookie];

}

UIWebView使用时间较长,只要在cookieStorage中安装了相应的cookie,每一遍就能够活动带上;

也就那样的缺陷是随着与H5的相互影响扩展,Cookie占用的半空中愈发大,每一回人机联作都夹带多量的cookie,不止扩大服务器端压力,也浪花销户的流量。比方每趟人机联作都夹带5kb的cookie内容,不过真的使用的只有两六百字节。

二. WKWebView

WKWebView相比于UIWebView:

速度快了一倍,内部存储器却裁减为原来的八分之四;

cookie不再是电动指引,需求手动设置;

交互作用特别通畅,举个例子app底部五个tabBar也都以网页的,在UIWebView下点击,整个H5页面都会闪白一下,不过在WKWebView下点击,多个tabBar效果与原生app效果尤其切近,不会有闪白现象。

增减了有的代理方法,更便利的打开商榷拦截和进程条展现

1.在开创的时候存放到WKUserScript中举行添加cookie

WKWebViewConfiguration *webConfig = [[WKWebViewConfiguration alloc] init];

// 设置偏幸设置

webConfig.preferences = [[WKPreferences alloc] init];

// 默认为0

webConfig.preferences.minimumFontSize = 10;

// 默许以为YES

webConfig.preferences.javaScriptEnabled = YES;

// 在iOS上默以为NO,表示无法自动通过窗口展开

webConfig.preferences.javaScriptCanOpenWindowsAutomatically = NO;

// web内容处理池

webConfig.processPool = [[WKProcessPool alloc] init];

// 将具备cookie以document.cookie = 'key=value';情势张开拼接

NSString *cookieValue = [@"document.cookie = 'fromapp=ios';document.cookie = 'channel=appstore';"];

// 加cookie给h5识别,证明在ios端展开该地点

WKUserContentController* userContentController = WKUserContentController.new;

WKUserScript * cookieScript = [[WKUserScript alloc]

initWithSource: cookieValue

injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];

[userContentController addUserScript:cookieScript];

webConfig.userContentController = userContentController;

WKWebView *wkWebView = [[WKWebView alloc] initWithFrame:frame configuration:webConfig];

wkWebView.UIDelegate = wkWebView;

wkWebView.navigationDelegate = wkWebView;

2.加载有些url的时候增添cookie

本文由新浦京81707con发布于首页,转载请注明出处:iOS的Cookie存取看我绝对够

关键词: 新浦京81707con ios 看我 Cookie

上一篇:面试经典面,面试基础题目

下一篇:没有了