欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

详解iOS App开发中Cookie的管理方法

程序员文章站 2023-12-15 20:04:58
一、何为cookie cookie是网站为了便是终端身份,保存在终端本地的用户凭证信息。cookie中的字段与意义由服务端进行定义。例如,当用户在某个网站进行了登录操作后...

一、何为cookie

cookie是网站为了便是终端身份,保存在终端本地的用户凭证信息。cookie中的字段与意义由服务端进行定义。例如,当用户在某个网站进行了登录操作后,服务端会将cookie信息返回给终端,终端会将这些信息进行保存,在下一次再次访问这个网站时,终端会将保存的cookie信息一并发送到服务端,服务端根据cookie信息是否有效来判断此用户是否可以自动登录。

二、ios中进行cookie管理的两个类

ios中进行http网络请求cookie管理主要由两个类负责,一个类是nshttpcookiestorage类,一个是nshttpcookie类。

1.nshttpcookiestorage

nshttpcookiestorage类采用单例的设计模式,其中管理着所有http请求的cookie信息,常用方法如下:

//获取单例对象
+ (nshttpcookiestorage *)sharedhttpcookiestorage;
//所有cookie数据数组 其中存放nshttpcookie对象
@property (nullable , readonly, copy) nsarray<nshttpcookie *> *cookies;
//手动设置一条cookie数据
- (void)setcookie:(nshttpcookie *)cookie;
//删除某条cookie信息
- (void)deletecookie:(nshttpcookie *)cookie;
//删除某个时间后的所有cookie信息 ios8后可用
- (nullable nsarray<nshttpcookie *> *)cookiesforurl:(nsurl *)url;
//获取某个特定url的所有cookie数据
- (void)removecookiessincedate:(nsdate *)date ns_available(10_10, 8_0);
//为某个特定的url设置cookie
- (void)setcookies:(nsarray<nshttpcookie *> *)cookies forurl:(nullable nsurl *)url maindocumenturl:(nullable nsurl *)maindocumenturl;
//cookie数据的接收协议
/*
枚举如下:
typedef ns_enum(nsuinteger, nshttpcookieacceptpolicy) {
    nshttpcookieacceptpolicyalways,//接收所有cookie信息
    nshttpcookieacceptpolicynever,//不接收所有cookie信息
    nshttpcookieacceptpolicyonlyfrommaindocumentdomain//只接收主文档域的cookie信息
};
*/
@property nshttpcookieacceptpolicy cookieacceptpolicy;
系统下面的两个通知与cookie管理有关:

//cookie数据的接收协议改变时发送的通知
foundation_export nsstring * const nshttpcookiemanageracceptpolicychangednotification;
//管理的cookie数据发生变化时发送的通知
foundation_export nsstring * const nshttpcookiemanagercookieschangednotification;

2.nshttpcookie

nshttpcookie是具体的http请求cookie数据对象,其中属性方法如下:

//下面两个方法用于对象的创建和初始化 都是通过字典进行键值设置
- (nullable instancetype)initwithproperties:(nsdictionary<nsstring *, id> *)properties;
+ (nullable nshttpcookie *)cookiewithproperties:(nsdictionary<nsstring *, id> *)properties;
//返回cookie数据中可用于添加http头字段的字典
+ (nsdictionary<nsstring *, nsstring *> *)requestheaderfieldswithcookies:(nsarray<nshttpcookie *> *)cookies;
//从指定的响应头和url地址中解析出cookie数据
+ (nsarray<nshttpcookie *> *)cookieswithresponseheaderfields:(nsdictionary<nsstring *, nsstring *> *)headerfields forurl:(nsurl *)url;
//cookie数据中的属性字典
@property (nullable, readonly, copy) nsdictionary<nsstring *, id> *properties;
//请求响应的版本
@property (readonly) nsuinteger version;
//请求相应的名称
@property (readonly, copy) nsstring *name;
//请求相应的值
@property (readonly, copy) nsstring *value;
//过期时间
@property (nullable, readonly, copy) nsdate *expiresdate;
//请求的域名
@property (readonly, copy) nsstring *domain;
//请求的路径
@property (readonly, copy) nsstring *path;
//是否是安全传输
@property (readonly, getter=issecure) bool secure;
//是否只发送http的服务
@property (readonly, getter=ishttponly) bool httponly;
//响应的文档
@property (nullable, readonly, copy) nsstring *comment;
//相应的文档url
@property (nullable, readonly, copy) nsurl *commenturl;
//服务端口列表
@property (nullable, readonly, copy) nsarray<nsnumber *> *portlist;

三、清除cookie
清除所有的cookie 方法:

nsurl *url = [nsurl urlwithstring:@"http://www.baidu.com"]; 
    if (url) { 
        nsarray *cookies = [[nshttpcookiestorage sharedhttpcookiestorage] cookiesforurl:url]; 
        for (int i = 0; i < [cookies count]; i++) { 
            nshttpcookie *cookie = (nshttpcookie *)[cookies objectatindex:i]; 
            [[nshttpcookiestorage sharedhttpcookiestorage] deletecookie:cookie]; 
             
        }  
  清除某一个特定的cookie方法:
nsarray * cookarray = [[nshttpcookiestorage sharedhttpcookiestorage] cookiesforurl:[nsurl urlwithstring:self.loadurl]]; nsstring * successcode = @""; for (nshttpcookie*cookie in cookarray) { if ([cookie.name isequaltostring:@"cookiename"]) { [[nshttpcookiestorage sharedhttpcookiestorage] deletecookie:cookie]; } } 

清除某一个url缓存的方法:
[[nsurlcache sharedurlcache] removecachedresponseforrequest:[nsurlrequest requestwithurl:url]]; 

清除所有缓存方法:       
[[nsurlcache sharedurlcache] removeallcachedresponses]; 

上一篇:

下一篇: