IOS网络请求iOS网络资料

iOS UserAgent与Cookie使用

2016-07-04  本文已影响1119人  Mr_Lucifer

前言

如果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];

}

在创建 需要加载的页面之前, 调用该方法, 进行修改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

二 Cookie

Session : 服务端要为特定的用户创建特定的Session,用于标识这个用户,并且跟踪用户的后续操作, 保存在服务器端.

Cookie : 大多数的应用 用 Cookie 来实现Session跟踪, 在 Cookie 里面记录一个Session ID,以后每次请求把这个会话ID发送到服务器, 保存在客户端.

详情传送门 : COOKIE和SESSION有什么区别?

 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>

如果采用的是默然失效时间, 那可以把Cookie存储起来, 下次打开App, 从新写入.

例:

NSData *cookiesData = [NSKeyedArchiver archivedDataWithRootObject: [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]];    
 //存储归档后的cookie
[[NSUserDefaults standardUserDefaults] setObject: cookiesData forKey: @"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");
    }
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];
 <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>
 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的介绍完.

以上 !

上一篇下一篇

猜你喜欢

热点阅读