前言
如果App项目中, 使用了webview, 后台需要知道该用户的来源, 来区分是网站端还是移动端, 从而进行一些后续处理, 这是需要判断依据 --UserAgent. 有时候项目中也需要对Cookie进行处理. 现在作者就聊一下这个问题.
一 UserAgent
User Agent : 用户代理,是指浏览器,它的信息包括硬件平台、系统软件、应用软件和用户个人偏好。iOS中 主要用于webview加载的页面, 给后台提供处理依据.
处理 : 用webview, 调用 JS 的navigator.userAgent, 获取原UserAgent, 与后台商定, 拼接字符串, 放入字典中, 存储.
#pragma mark - 修改UserAgent
-(void)reviseUserAgent{
UIWebView *tempWebView = [[UIWebView alloc] initWithFrame:CGRectZero];
NSString *secretAgent = [tempWebView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
NSString *newUagent = [NSString stringWithFormat:@"%@ iOSApp",secretAgent];
NSDictionary *dictionary = [[NSDictionary alloc]
initWithObjectsAndKeys:newUagent, @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
[[NSUserDefaults standardUserDefaults] synchronize];
}
在创建 需要加载的页面之前, 调用该方法, 进行修改UserAgent.
结果打印 :
NSLog(@"NewUserAgent : %@", [self.webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"]);
// 输出结果:
NewUserAgent :Mozilla/5.0 (iPhone; CPU iPhone OS 9_3 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13E230 iOSApp
WKWebView处理
// 获取
[self.webView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
NSLog(@"UserAgent: %@", result);
}];
// 设置
[self.webView setCustomUserAgent:@"newUagent"];
二 Cookie
- 先说下 Cookie 与 Session
Session : 服务端要为特定的用户创建特定的Session,用于标识这个用户,并且跟踪用户的后续操作, 保存在服务器端.
Cookie : 大多数的应用 用 Cookie 来实现Session跟踪, 在 Cookie 里面记录一个Session ID,以后每次请求把这个会话ID发送到服务器, 保存在客户端.
详情传送门 : COOKIE和SESSION有什么区别?
-
1. 获取Cookie
可以在afn加载成功 或者 webview 加载完成处打印.
NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
for (NSHTTPCookie *tempCookie in cookies) {
//打印获得的cookie
NSLog(@"GetCookie: %@", tempCookie);
}
打印结果举例 :
GetCookie: <NSHTTPCookie version:0 name:"BDSVRTM" value:"12" expiresDate:(null) created:2016-07-04 05:43:54 +0000 sessionOnly:TRUE domain:"www.baidu.com" path:"/" isSecure:FALSE>
-
2. 存储Cookie 与 重新写入Cookie
默认cookies失效时间是直到关闭浏览器,cookie失效, 也就是App退出, cookie就失效, 但是可以设置失效时间, 这部分最好由后台来处理, 既然加载的是页面, 那就是页面的布局设计由后台来完成, 所以App程序里控制的越少越好.
如果采用的是默然失效时间, 那可以把Cookie存储起来, 下次打开App, 从新写入.
例:
- 存储 : 需要对cookie进行归档处理, 转换为NSData, 采用NSUserDefaults存储. 可以在afn加载成功 或者 webview 加载完成处选择存储.
NSData *cookiesData = [NSKeyedArchiver archivedDataWithRootObject: [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]];
//存储归档后的cookie
[[NSUserDefaults standardUserDefaults] setObject: cookiesData forKey: @"cookie"];
- 写入 : 取出保存的cookie, 对取出的cookie进行反归档处理, 重新设置cookie .
//取出保存的cookie
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
//对取出的cookie进行反归档处理
NSArray *cookies = [NSKeyedUnarchiver unarchiveObjectWithData:[userDefaults objectForKey:@"cookie"]];
if (cookies) {
NSLog(@"有cookie");
//设置cookie
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (id cookie in cookies) {
[cookieStorage setCookie:(NSHTTPCookie *)cookie];
}
}else{
NSLog(@"无cookie");
}
-
3. 删除
可以删除特定cookie
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
//删除cookie
for (NSHTTPCookie *tempCookie in cookies) {
if ([tempCookie.name isEqualToString:@"dbsource"]) {
[cookieStorage deleteCookie:tempCookie];
}
}
如存储在NSUserDefaults中, 删除整个cookies.
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"cookie"];
[[NSUserDefaults standardUserDefaults] synchronize];
4. App程序写入需要的cookie
举例 (写入如下需求的cookie):
<NSHTTPCookie version:0 name:"Baidu" value:"99" expiresDate: 2016-08-14 00:00:00 +0000 created:2016-07-04 05:43:54 +0000 sessionOnly:FALSE domain:"www.baidu.com" path:"/" isSecure:FALSE>
- 写入(需要求的)cookie :
NSMutableDictionary *cookiePropertiesDic = [NSMutableDictionary dictionary];
[cookiePropertiesDic setObject:@"Baidu" forKey:NSHTTPCookieName];
[cookiePropertiesDic setObject:@"99" forKey:NSHTTPCookieValue];
[cookiePropertiesDic setObject:@"www.baidu.com" forKey:NSHTTPCookieDomain];
[cookiePropertiesDic setObject:@"www.baidu.com" forKey:NSHTTPCookieOriginURL];
[cookiePropertiesDic setObject:@"/" forKey:NSHTTPCookiePath]; [cookiePropertiesDic setObject:@"0" forKey:NSHTTPCookieVersion];
[cookiePropertiesDic setObject:@"2016-08-14 00:00:00 +0000" forKey:NSHTTPCookieExpires];
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookiePropertiesDic];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
参考 : IT985博客
工程中对UserAgent与Cookie的介绍完.
以上 !