搜索
您的当前位置:首页正文

iOS 中 Cookie 一些可能需要知道的地方

来源:知库网

昨天有同事和我说,打一个不同负载的包分别测试下咯,我第一反应有点懵,这个平常真心接触的有点少,于是带着疑惑去了解下,首先可以肯定的 是这个和 Cookie 相关的。

一、Cookie 相关的了解

  • Cookie 的理解

    • Cookie 是某些网站为了辨别用户身份而储存在用户本地终端上的数据。
    • Cookie 就是服务器暂存放在客户端上的一笔资料,好让服务器用于辨别客户端。
  • Cookie 工作原理


    Cookie 的工作原理,
  • Cookie 和 负载均衡
Cookie 和 自己的理解对不同的负载

#######备注参考:

PS : 会话 cookie 和持久性 cookie 的区别,说白了就是是否包含 到期日期 确定。


二、iOS 中 具体的用 Cookie

我之前用到的地方,主要是写 Cookie 的咯,我们为了测试特意在上线之前再加了一个预发布的环境,就是为了模拟线上环境,其中就是对 Cookie 做标记的处理的。当然有写,肯定还有读 Cookie的。

  • NSHTTPCookie: cookie 对象
  • NSHTTPCookieStorage : 管理所有 NSHTTPCookie 的对象

对上述两个对象好好了解下,就差不多了。

#######写 Cookie

// URL 
NSURL *url = 
// propertiesDic,可特殊设置, 改变其也是看这里
NSDictionary *propertiesDic = @{
                                 NSHTTPCookieName: @"testName",
                                 NSHTTPCookieValue:@"testValue",
                                 NSHTTPCookieDomain: url.host,
                                 NSHTTPCookiePath: url.path
                                };
NSHTTPCookie *cookie = [[NSHTTPCookie alloc] initWithProperties: propertiesDic];
// 设置 Cookie
[[NSHTTPCookieStorage sharedHTTPCookieStorage]setCookie:cookie];
  • NSHTTPCookie 可中可改变的属性,特变注意前面几个。
FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookieName;
FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookieValue;
FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookieOriginURL;
FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookieVersion;
FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookieDomain;
FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookiePath;
FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookieSecure;
FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookieExpires;
FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookieComment;
FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookieCommentURL;
FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookieDiscard;
FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookieMaximumAge;
FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookiePort;

#######读 Cookie

NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *cookie in [cookieStorage cookies]) {
       NSLog(@"%@", cookie);
}

#######清空 Cookie

NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *cookie in [cookieStorage cookies]) {
       [cookieStorage deleteCookie: cookie];
}

说白了就是下面属性 和 方法的运用:

/*!
    @method cookies
    @abstract Get all the cookies
    @result An NSArray of NSHTTPCookies
*/
@property (nullable , readonly, copy) NSArray<NSHTTPCookie *> *cookies;

/*!
    @method setCookie:
    @abstract Set a cookie
    @discussion The cookie will override an existing cookie with the
    same name, domain and path, if any.
*/
- (void)setCookie:(NSHTTPCookie *)cookie;

/*!
    @method deleteCookie:
    @abstract Delete the specified cookie
*/
- (void)deleteCookie:(NSHTTPCookie *)cookie;

整体来说,此次是更多的是 Cookie 和 负载相关的了解,对 iOS 之外的更多理解才是重点,也是需要额外注意的。

Top