美文网首页
iOS 中 Cookie 一些可能需要知道的地方

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

作者: 天空中的球 | 来源:发表于2017-05-22 12:01 被阅读587次

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

    一、Cookie 相关的了解

    • Cookie 的理解

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


      Cookie 的工作原理, 图片源自:[cookie原理说明](https://www.qcloud.com/document/product/214/2737)
    • Cookie 和 负载均衡
    Cookie 和 负载,图片来源:[【负载均衡小知识】](https://sanwen8.cn/p/3fdKpiJ.html)

    当然 负载均衡 有很多方方面的,此处我只作为了解,了解下我们 App 端与其相关联的。

    自己的理解对不同的负载

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

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


    二、iOS 中 具体的用 Cookie

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

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

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

    #######写 Cookie

    // URL 
    NSURL *url = @"http://www.test.com";
    // 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 之外的更多理解才是重点,也是需要额外注意的。

    #######备注参考:Cookie机制以及cookie在iOS中使用介绍

    相关文章

      网友评论

          本文标题:iOS 中 Cookie 一些可能需要知道的地方

          本文链接:https://www.haomeiwen.com/subject/ftaexxtx.html