詳細解析幾個和網(wǎng)絡(luò)請求有關(guān)的類(十五) —— NSHTTPCookieStorage(一)

版本記錄

版本號 時間
V1.0 2018.03.15

前言

我們做APP發(fā)起網(wǎng)絡(luò)請求靶衍,一般都是使用框架邀层,這些框架的底層也都是蘋果的API棕洋,接下來幾篇就一起來看一下和網(wǎng)絡(luò)有關(guān)的幾個類微饥。感興趣的可以看上面幾篇文章逗扒。
1. 詳細解析幾個和網(wǎng)絡(luò)請求有關(guān)的類 (一) —— NSURLSession
2. 詳細解析幾個和網(wǎng)絡(luò)請求有關(guān)的類(二) —— NSURLRequest和NSMutableURLRequest
3. 詳細解析幾個和網(wǎng)絡(luò)請求有關(guān)的類(三) —— NSURLConnection
4. 詳細解析幾個和網(wǎng)絡(luò)請求有關(guān)的類(四) —— NSURLSession和NSURLConnection的區(qū)別
5. 詳細解析幾個和網(wǎng)絡(luò)請求有關(guān)的類(五) —— 關(guān)于NSURL加載系統(tǒng)(一)
6. 詳細解析幾個和網(wǎng)絡(luò)請求有關(guān)的類(六) —— 使用NSURLSession(二)
7. 詳細解析幾個和網(wǎng)絡(luò)請求有關(guān)的類(七) —— URL數(shù)據(jù)的編碼和解碼(三)
8. 詳細解析幾個和網(wǎng)絡(luò)請求有關(guān)的類(八) —— 處理重定向和其他請求更改(四)
9. 詳細解析幾個和網(wǎng)絡(luò)請求有關(guān)的類(九) —— 身份驗證挑戰(zhàn)和TLS鏈驗證(五)
10. 詳細解析幾個和網(wǎng)絡(luò)請求有關(guān)的類(十) —— 理解獲取緩存(六)
11. 詳細解析幾個和網(wǎng)絡(luò)請求有關(guān)的類(十一) —— Cookies和自定義協(xié)議(七)
12. 詳細解析幾個和網(wǎng)絡(luò)請求有關(guān)的類(十二) —— URL Session的生命周期(八)
13. 詳細解析幾個和網(wǎng)絡(luò)請求有關(guān)的類(十三) —— NSURLResponse(一)
14. 詳細解析幾個和網(wǎng)絡(luò)請求有關(guān)的類(十四) —— NSHTTPCookie(一)

回顧

上一篇講述關(guān)于NSHTTPCookie,下面這篇我們就主要看一下NSHTTPCookieStorage欠橘。


基本信息

首先我們看一下該類的基本信息矩肩。

1. Overview

管理cookie存儲的單例對象(共享實例)。

每個cookie都由NSHTTPCookie類的實例表示肃续。 通常黍檩,Cookie在所有應(yīng)用程序之間共享,并跨越進程邊界保持同步始锚。 Session cookie(cookie對象的sessionOnly方法返回true)對于單個進程是本地的刽酱,不共享。

iOS Note:在iOS中cookie在程序之間是不共享的瞧捌。

注意:對cookie接受策略所做的更改會影響使用cookie存儲的所有當前正在運行的應(yīng)用程序棵里。

當你訪問一個網(wǎng)站時,NSURLRequest都會幫你主動記錄下來你訪問的站點設(shè)置的Cookie姐呐,如果 Cookie 存在的話殿怜,會把這些信息放在 NSHTTPCookieStorage 容器中共享,當你下次再訪問這個站點時曙砂,NSURLRequest會拿著上次保存下來了的Cookie繼續(xù)去請求头谜。

NSHTTPCookieStorage 實現(xiàn)了一個管理Cookie的單例對象(只有一個實例),每個Cookie都是NSHTTPCookie類的實例鸠澈,作為一個規(guī)則乔夯,Cookie在所有應(yīng)用之間共享并在不同進程之間保持同步。Session Cookie(一個isSessionOnly方法返回YES的Cookie)只能在單一進程中使用款侵。

2. Thread Safety - 線程安全

macOS 10.9及更高版本和iOS 7及更高版本中,NSHTTPCookieStorage是線程安全的侧纯。


Topics

1. Creating and Initializing a Cookie Storage Object - 創(chuàng)建和初始化Cookie Storage對象

  • - initWithStorageLocation:
    • 返回一個具有給定文件系統(tǒng)位置的初始化NSHTTPCookieStorage對象新锈,以將Cookie信息存儲在磁盤上。

2. Getting the Shared Cookie Storage Object - 獲取共享的Cookie Storage對象

3. Getting and Setting the Cookie Accept Policy - 獲取和設(shè)置Cookie Accept Policy

4. Adding and Removing Cookies - 增加和移除Cookie

5. Retrieving Cookies - 檢索Cookies

6. Constants

  • NSHTTPCookieAcceptPolicy
    • NSHTTPCookieAcceptPolicy指定由NSHTTPCookieStorage類實現(xiàn)的Cookie接受策略海渊。

7. Notifications


API 文檔

NSHTTPCookieStorage分類

接下來我們看一下API文檔,我在里面增加了注釋徙菠。

#import <Foundation/NSObject.h>
#import <Foundation/NSNotification.h>

@class NSArray<ObjectType>;
@class NSHTTPCookie;
@class NSURL;
@class NSDate;
@class NSURLSessionTask;
@class NSSortDescriptor;

NS_ASSUME_NONNULL_BEGIN

/*!
    @enum NSHTTPCookieAcceptPolicy
    @abstract Values for the different cookie accept policies
    @constant NSHTTPCookieAcceptPolicyAlways Accept all cookies
    @constant NSHTTPCookieAcceptPolicyNever Reject all cookies
    @constant NSHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain Accept cookies
    only from the main document domain
*/
// 這里是不同cookie接受策略的值
typedef NS_ENUM(NSUInteger, NSHTTPCookieAcceptPolicy) {
    // 接受所有的cookie
    NSHTTPCookieAcceptPolicyAlways,

    // 拒絕所有的cookie
    NSHTTPCookieAcceptPolicyNever,
    
    //只接受來自主文件域的cookie
    NSHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain
};


@class NSHTTPCookieStorageInternal;

/*!
    @class NSHTTPCookieStorage 
    @discussion NSHTTPCookieStorage implements a singleton object (shared
    instance) which manages the shared cookie store.  It has methods
    to allow clients to set and remove cookies, and get the current
    set of cookies.  It also has convenience methods to parse and
    generate cookie-related HTTP header fields.
*/
// NSHTTPCookieStorage實現(xiàn)了一個管理共享cookie存儲的單例對象(共享實例)讯沈。 它具有允許客戶設(shè)置和刪除Cookie的方法,并獲得當前的一組Cookie婿奔。 它還具有解析和生成與cookie相關(guān)的HTTP頭字段的便捷方法缺狠。

@interface NSHTTPCookieStorage : NSObject
{
    @private
    NSHTTPCookieStorageInternal *_internal;
}

/*!
    @property sharedHTTPCookieStorage
    @abstract Get the shared cookie storage in the default location.
    @result The shared cookie storage
    @discussion Starting in OS X 10.11, each app has its own sharedHTTPCookieStorage singleton, 
    which will not be shared with other applications.
*/
// 獲取在默認位置的共享cookie storage。從OS X 10.11開始萍摊,
// 每一個app都有自己的sharedHTTPCookieStorage單例挤茄,不會和其他程序分享。

@property(class, readonly, strong) NSHTTPCookieStorage *sharedHTTPCookieStorage;

/*!
    @method sharedCookieStorageForGroupContainerIdentifier:
    @abstract Get the cookie storage for the container associated with the specified application group identifier
    @param identifier The application group identifier
    @result A cookie storage with a persistent store in the application group container
    @discussion By default, applications and associated app extensions have different data containers, which means
    that the sharedHTTPCookieStorage singleton will refer to different persistent cookie stores in an application and
    any app extensions that it contains. This method allows clients to create a persistent cookie storage that can be
    shared among all applications and extensions with access to the same application group. Subsequent calls to this
    method with the same identifier will return the same cookie storage instance.
 */
// 獲取與指定應(yīng)用程序組標識符關(guān)聯(lián)的容器的Cookie存儲记餐。
// identifier:應(yīng)用程序組標識符
// result:在應(yīng)用程序組容器中具有持久性存儲的Cookie存儲
// 默認情況下驮樊,應(yīng)用程序和關(guān)聯(lián)的應(yīng)用程序擴展具有不同的數(shù)據(jù)容器,
// 這意味著sharedHTTPCookieStorage單例將引用應(yīng)用程序中的不同持久性cookie
// 存儲以及它包含的任何應(yīng)用程序擴展片酝。 此方法允許客戶端創(chuàng)建持久性cookie存儲囚衔,
// 可以在所有應(yīng)用程序和擴展中共享,并訪問相同的應(yīng)用程序組雕沿。 隨后使用相同標識符調(diào)用
// 此方法將返回相同的cookie存儲實例练湿。

+ (NSHTTPCookieStorage *)sharedCookieStorageForGroupContainerIdentifier:(NSString *)identifier API_AVAILABLE(macos(10.11), ios(9.0), watchos(2.0), tvos(9.0));

/*!
    @abstract Get all the cookies
    @result An NSArray of NSHTTPCookies
*/
// 獲取所有的cookies

@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.
*/
// 設(shè)置cookie,該cookie將覆蓋具有相同名稱审轮,域和路徑的現(xiàn)有cookie肥哎,如果有的話。

- (void)setCookie:(NSHTTPCookie *)cookie;

/*!
    @method deleteCookie:
    @abstract Delete the specified cookie
*/
// 刪除指定的cookie
- (void)deleteCookie:(NSHTTPCookie *)cookie;

/*!
 @method removeCookiesSince:
 @abstract Delete all cookies from the cookie storage since the provided date.
 */
// 刪除指定日期cookie storage中所有的cookie

- (void)removeCookiesSinceDate:(NSDate *)date API_AVAILABLE(macos(10.10), ios(8.0), watchos(2.0), tvos(9.0));

/*!
    @method cookiesForURL:
    @abstract Returns an array of cookies to send to the given URL.
    @param URL The URL for which to get cookies.
    @result an NSArray of NSHTTPCookie objects.
    @discussion The cookie manager examines the cookies it stores and
    includes those which should be sent to the given URL. You can use
    <tt>+[NSCookie requestHeaderFieldsWithCookies:]</tt> to turn this array
    into a set of header fields to add to a request.
*/
// 返回要發(fā)送到給定URL的Cookie數(shù)組疾渣。
// Cookie管理器檢查它存儲的cookie篡诽,其中包含應(yīng)發(fā)送到給定URL的cookie。 
// 您可以使用<tt> + [NSCookie requestHeaderFieldsWithCookies:] </ tt>
// 將此數(shù)組轉(zhuǎn)換為一組頭字段以添加到請求中

- (nullable NSArray<NSHTTPCookie *> *)cookiesForURL:(NSURL *)URL;

/*!
    @method setCookies:forURL:mainDocumentURL:
    @abstract Adds an array cookies to the cookie store, following the
    cookie accept policy.
    @param cookies The cookies to set.
    @param URL The URL from which the cookies were sent.
    @param mainDocumentURL The main document URL to be used as a base for the "same
    domain as main document" policy.
    @discussion For mainDocumentURL, the caller should pass the URL for
    an appropriate main document, if known. For example, when loading
    a web page, the URL of the main html document for the top-level
    frame should be passed. To save cookies based on a set of response
    headers, you can use <tt>+[NSCookie
    cookiesWithResponseHeaderFields:forURL:]</tt> on a header field
    dictionary and then use this method to store the resulting cookies
    in accordance with policy settings.
*/
// 將cookie數(shù)組添加到cookie store榴捡,遵守cookie 接受策略杈女。 
// mainDocumentURL: 主要文檔URL用作“與主文檔相同的域”策略的基礎(chǔ)
// 對于mainDocumentURL,調(diào)用者應(yīng)該傳遞適當主文檔的URL(如果知道)。 
// 例如达椰,在加載網(wǎng)頁時翰蠢,應(yīng)傳遞頂層框架的主要html文檔的URL。 
// 要根據(jù)一組響應(yīng)頭保存cookie啰劲,您可以在頭字段字典中使用<tt> + [NSCookie cookiesWithResponseHeaderFields:forURL:] </ tt>梁沧,
// 然后使用此方法根據(jù)策略設(shè)置存儲生成的cookie。

- (void)setCookies:(NSArray<NSHTTPCookie *> *)cookies forURL:(nullable NSURL *)URL mainDocumentURL:(nullable NSURL *)mainDocumentURL;

/*!
    @abstract The cookie accept policy preference of the
    receiver.
*/
// 接收者的cookie可接受策略蝇裤。

@property NSHTTPCookieAcceptPolicy cookieAcceptPolicy;

/*!
  @method sortedCookiesUsingDescriptors:
  @abstract Returns an array of all cookies in the store, sorted according to the key value and sorting direction of the NSSortDescriptors specified in the parameter.
  @param sortOrder an array of NSSortDescriptors which represent the preferred sort order of the resulting array.
  @discussion proper sorting of cookies may require extensive string conversion, which can be avoided by allowing the system to perform the sorting.  This API is to be preferred over the more generic -[NSHTTPCookieStorage cookies] API, if sorting is going to be performed.
*/
// 返回存儲區(qū)中所有Cookie的數(shù)組廷支,根據(jù)參數(shù)中指定的NSSortDescriptors的鍵值和排序方向進行排序
// sortOrder:一個NSSortDescriptors數(shù)組,表示結(jié)果數(shù)組的首選排序順序
// 恰當?shù)胤诸恈ookies可能需要大量的字符串轉(zhuǎn)換猖辫,這可以通過允許系統(tǒng)執(zhí)行分類來避免酥泞。 
// 如果要執(zhí)行排序,則此API比更通用的[NSHTTPCookieStorage cookie] API更受歡迎

- (NSArray<NSHTTPCookie *> *)sortedCookiesUsingDescriptors:(NSArray<NSSortDescriptor *> *) sortOrder API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));

@end

NSHTTPCookieStorage分類NSURLSessionTaskAdditions

這個是針對給定任務(wù)的NSHTTPCookieStorage分類

@interface NSHTTPCookieStorage (NSURLSessionTaskAdditions)

// 根據(jù)給定的task存儲Cookie
- (void)storeCookies:(NSArray<NSHTTPCookie *> *)cookies forTask:(NSURLSessionTask *)task API_AVAILABLE(macos(10.10), ios(8.0), watchos(2.0), tvos(9.0));

// 獲取指定任務(wù)的Cookie啃憎,并處理完成回調(diào)
- (void)getCookiesForTask:(NSURLSessionTask *)task completionHandler:(void (^) (NSArray<NSHTTPCookie *> * _Nullable cookies))completionHandler API_AVAILABLE(macos(10.10), ios(8.0), watchos(2.0), tvos(9.0));

@end

/*!
    @const NSHTTPCookieManagerAcceptPolicyChangedNotification
    @discussion Name of notification that should be posted to the
    distributed notification center whenever the accept cookies
    preference is changed
*/
FOUNDATION_EXPORT NSNotificationName const NSHTTPCookieManagerAcceptPolicyChangedNotification;

/*!
    @const NSHTTPCookieManagerCookiesChangedNotification
    @abstract Notification sent when the set of cookies changes
*/
FOUNDATION_EXPORT NSNotificationName const NSHTTPCookieManagerCookiesChangedNotification;

NS_ASSUME_NONNULL_END

后記

本篇主要講述了NSHTTPCookieStorage這個類的使用芝囤,喜歡的給個贊和關(guān)注哈~~~~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市辛萍,隨后出現(xiàn)的幾起案子悯姊,更是在濱河造成了極大的恐慌,老刑警劉巖贩毕,帶你破解...
    沈念sama閱讀 217,185評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件悯许,死亡現(xiàn)場離奇詭異,居然都是意外死亡辉阶,警方通過查閱死者的電腦和手機先壕,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評論 3 393
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來谆甜,“玉大人垃僚,你說我怎么就攤上這事」嫒瑁” “怎么了谆棺?”我有些...
    開封第一講書人閱讀 163,524評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長罕袋。 經(jīng)常有香客問我改淑,道長,這世上最難降的妖魔是什么浴讯? 我笑而不...
    開封第一講書人閱讀 58,339評論 1 293
  • 正文 為了忘掉前任朵夏,我火速辦了婚禮,結(jié)果婚禮上榆纽,老公的妹妹穿的比我還像新娘侍郭。我一直安慰自己询吴,他們只是感情好,可當我...
    茶點故事閱讀 67,387評論 6 391
  • 文/花漫 我一把揭開白布亮元。 她就那樣靜靜地躺著,像睡著了一般唠摹。 火紅的嫁衣襯著肌膚如雪爆捞。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,287評論 1 301
  • 那天勾拉,我揣著相機與錄音煮甥,去河邊找鬼。 笑死藕赞,一個胖子當著我的面吹牛成肘,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播斧蜕,決...
    沈念sama閱讀 40,130評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼双霍,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了批销?” 一聲冷哼從身側(cè)響起洒闸,我...
    開封第一講書人閱讀 38,985評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎均芽,沒想到半個月后丘逸,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,420評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡掀宋,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,617評論 3 334
  • 正文 我和宋清朗相戀三年深纲,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片劲妙。...
    茶點故事閱讀 39,779評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡湃鹊,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出是趴,到底是詐尸還是另有隱情涛舍,我是刑警寧澤,帶...
    沈念sama閱讀 35,477評論 5 345
  • 正文 年R本政府宣布唆途,位于F島的核電站富雅,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏肛搬。R本人自食惡果不足惜没佑,卻給世界環(huán)境...
    茶點故事閱讀 41,088評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望温赔。 院中可真熱鬧蛤奢,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至痹屹,卻和暖如春章郁,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背志衍。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評論 1 269
  • 我被黑心中介騙來泰國打工暖庄, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人楼肪。 一個月前我還...
    沈念sama閱讀 47,876評論 2 370
  • 正文 我出身青樓培廓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親春叫。 傳聞我的和親對象是個殘疾皇子肩钠,可洞房花燭夜當晚...
    茶點故事閱讀 44,700評論 2 354

推薦閱讀更多精彩內(nèi)容