2016.06.29
当前一些公司为了快速出一款app,很多时候采用UINavigationController+WebView或者NavigationController+UITabbarVC+WebView的方式,这样就不可避免的需要使用cookie与Html5交互,下面讲述几种常用情景下cookie的添加方法:
一. UIWebView:
+ (void)saveCookies {
NSMutableDictionary *fromappDict = [NSMutableDictionary dictionary];
[fromappDict setObject:@"fromapp" forKey:NSHTTPCookieName];
[fromappDict setObject:@"ios" forKey:NSHTTPCookieValue];
[fromappDict setObject:kDomain forKey:NSHTTPCookieDomain];
[fromappDict setObject:kDomain forKey:NSHTTPCookieOriginURL];
[fromappDict setObject:@"/" forKey:NSHTTPCookiePath];
[fromappDict setObject:@"0" forKey:NSHTTPCookieVersion];
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:fromappDict];
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage]
[cookieStorage setCookie:cookie];
}
- UIWebView使用时间较长,只要在cookieStorage中设置了相应的cookie,每次就会自动带上;
- 但是这样的弊端是随着与H5的交互增加,Cookie占用的空间越来越大,每次交互都夹带大量的cookie,不仅增加服务器端压力,也浪费用户的流量。比如每次交互都夹带5kb的cookie内容,但是真正用到的只有两三百字节。
二. WKWebView
WKWebViewConfiguration *webConfig = [[WKWebViewConfiguration alloc] init];
webConfig.preferences = [[WKPreferences alloc] init];
webConfig.preferences.minimumFontSize = 10;
webConfig.preferences.javaScriptEnabled = YES;
webConfig.preferences.javaScriptCanOpenWindowsAutomatically = NO;
webConfig.processPool = [[WKProcessPool alloc] init];
#warning 然而这里的单引号一定要注意是英文的,不要问我为什么告诉你这个(手动微笑)
NSString *cookieValue = @"document.cookie = 'fromapp=ios';document.cookie = 'channel=appstore';";
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;
如果WKWebView在加载url的时候需要添加cookie,需要先手动获取当前NSHTTPCookieStorage中的所有cookie,然后将cookie放到NSMutableURLRequest请求头中
- (void)loadRequestWithUrlString:(NSString *)urlString {
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];
}
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];
}
3.AFNetworking
AFNetworking存取cookie就比较常见了,话不多说,都在代码里了
AFHTTPRequestOperationManager *operationManager = [AFHTTPRequestOperationManager manager];
NSMutableDictionary *cookieDic = [NSMutableDictionary dictionary];
#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];
}
for (NSString *key in cookieDic) {
NSString *appendString = [NSString stringWithFormat:@"%@=%@;", key, [cookieDic valueForKey:key]];
[cookieValue appendString:appendString];
}
[operationManager.requestSerializer setValue:cookieValue forHTTPHeaderField:@"Cookie"];
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);
}];