詳細(xì)解析幾個(gè)和網(wǎng)絡(luò)請(qǐng)求有關(guān)的類(lèi)(二十一) —— NSURLCredentialStorage(一)

版本記錄

版本號(hào) 時(shí)間
V1.0 2018.03.18

前言

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

回顧

上一篇講述了NSURLCredential這個(gè)類(lèi)的詳細(xì)信息以及一些注意要點(diǎn),下面這篇我們就主要看一下NSURLCredentialStorage。


Overview

一個(gè)管理credential存儲(chǔ)的對(duì)象。

首先看一下該類(lèi)的基本信息。


Topics

1. Getting the Credential Storage - 獲取Credential Storage

2. Getting and Setting Default Credentials - 獲取和設(shè)置Default Credentials

3. Adding and Removing Credentials - 添加和移除憑證

4. Retrieving Credentials - 檢索憑據(jù)

5. Constants

6. Notifications


API

下面我們看一下該類(lèi)的API文檔

1. NSURLCredentialStorage本類(lèi)

/*!
    @class NSURLCredentialStorage
    @discussion NSURLCredentialStorage implements a singleton object (shared instance) which manages the shared credentials cache. Note: Whereas in Mac OS X any application can access any credential with a persistence of NSURLCredentialPersistencePermanent provided the user gives permission, in iPhone OS an application can access only its own credentials.
*/
// NSURLCredentialStorage實(shí)現(xiàn)一個(gè)管理共享憑證緩存的單例對(duì)象(共享實(shí)例)。 
// 注意:在Mac OS X中,任何應(yīng)用程序都可以通過(guò)持久化NSURLCredentialPersistencePermanent
// 訪問(wèn)任何憑據(jù),只要用戶(hù)授予權(quán)限即可,在iPhone OS中,應(yīng)用程序只能訪問(wèn)自己的憑據(jù)。

@interface NSURLCredentialStorage : NSObject
{
    @private
    NSURLCredentialStorageInternal *_internal;
}

/*!
    @property sharedCredentialStorage
    @abstract Get the shared singleton authentication storage
    @result the shared authentication storage
*/
// 獲取共享驗(yàn)證存儲(chǔ)的單例

@property (class, readonly, strong) NSURLCredentialStorage *sharedCredentialStorage;

/*!
    @method credentialsForProtectionSpace:
    @abstract Get a dictionary mapping usernames to credentials for the specified protection space.
    @param space An NSURLProtectionSpace indicating the protection space for which to get credentials
    @result A dictionary where the keys are usernames and the values are the corresponding NSURLCredentials.
*/
// 獲取將用戶(hù)名映射到指定保護(hù)空間的憑證的字典

- (nullable NSDictionary<NSString *, NSURLCredential *> *)credentialsForProtectionSpace:(NSURLProtectionSpace *)space;

/*!
    @abstract Get a dictionary mapping NSURLProtectionSpaces to dictionaries which map usernames to NSURLCredentials
    @result an NSDictionary where the keys are NSURLProtectionSpaces
    and the values are dictionaries, in which the keys are usernames
    and the values are NSURLCredentials
*/
// 獲取將NSURLProtectionSpaces映射到字典,該字典將用戶(hù)名映射到NSURLCredentials。

@property (readonly, copy) NSDictionary<NSURLProtectionSpace *, NSDictionary<NSString *, NSURLCredential *> *> *allCredentials;

/*!
    @method setCredential:forProtectionSpace:
    @abstract Add a new credential to the set for the specified protection space or replace an existing one.
    @param credential The credential to set.
    @param space The protection space for which to add it. 
    @discussion Multiple credentials may be set for a given protection space, but each must have
    a distinct user. If a credential with the same user is already set for the protection space,
    the new one will replace it.
*/
// 為指定的保護(hù)空間添加一個(gè)新的憑證,或替換現(xiàn)有的憑證。
// 可以為給定的保護(hù)空間設(shè)置多個(gè)憑證,但每個(gè)憑證都必須具有不同的用戶(hù)。 
// 如果已經(jīng)為保護(hù)空間設(shè)置了具有相同用戶(hù)的憑證,則新憑證將替換它

- (void)setCredential:(NSURLCredential *)credential forProtectionSpace:(NSURLProtectionSpace *)space;

/*!
    @method removeCredential:forProtectionSpace:
    @abstract Remove the credential from the set for the specified protection space.
    @param credential The credential to remove.
    @param space The protection space for which a credential should be removed
    @discussion The credential is removed from both persistent and temporary storage. A credential that
    has a persistence policy of NSURLCredentialPersistenceSynchronizable will fail.  
    See removeCredential:forProtectionSpace:options.
*/
// 從指定保護(hù)空間的集合中刪除證書(shū)。該證書(shū)從永久和臨時(shí)存儲(chǔ)中刪除。 
// 擁有NSURLCredentialPersistenceSynchronizable持久性策略的憑證將失敗。 
// 請(qǐng)參閱removeCredential:forProtectionSpace:選項(xiàng)

- (void)removeCredential:(NSURLCredential *)credential forProtectionSpace:(NSURLProtectionSpace *)space;

/*!
 @method removeCredential:forProtectionSpace:options
 @abstract Remove the credential from the set for the specified protection space based on options.
 @param credential The credential to remove.
 @param space The protection space for which a credential should be removed
 @param options A dictionary containing options to consider when removing the credential.  This should
 be used when trying to delete a credential that has the NSURLCredentialPersistenceSynchronizable policy.
 Please note that when NSURLCredential objects that have a NSURLCredentialPersistenceSynchronizable policy
 are removed, the credential will be removed on all devices that contain this credential.
 @discussion The credential is removed from both persistent and temporary storage.
 */
// 根據(jù)選項(xiàng)從指定保護(hù)空間的集合中刪除證書(shū)。
// @param options 包含刪除憑證時(shí)要考慮的選項(xiàng)的字典。 嘗試刪除具有NSURLCredentialPersistenceSynchronizable
// 策略的憑證時(shí)應(yīng)使用此選項(xiàng)。 請(qǐng)注意,當(dāng)NSURLCredential對(duì)象具有NSURLCredentialPersistenceSynchronizable
// 策略時(shí),將刪除包含此憑據(jù)的所有設(shè)備上的憑證。

- (void)removeCredential:(NSURLCredential *)credential forProtectionSpace:(NSURLProtectionSpace *)space options:(nullable NSDictionary<NSString *, id> *)options API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0));

/*!
    @method defaultCredentialForProtectionSpace:
    @abstract Get the default credential for the specified protection space.
    @param space The protection space for which to get the default credential.
*/
// 獲取指定保護(hù)空間的默認(rèn)憑證

- (nullable NSURLCredential *)defaultCredentialForProtectionSpace:(NSURLProtectionSpace *)space;

/*!
    @method setDefaultCredential:forProtectionSpace:
    @abstract Set the default credential for the specified protection space.
    @param credential The credential to set as default.
    @param space The protection space for which the credential should be set as default.
    @discussion If the credential is not yet in the set for the protection space, it will be added to it.
*/
// 為指定的保護(hù)空間設(shè)置默認(rèn)憑證

- (void)setDefaultCredential:(NSURLCredential *)credential forProtectionSpace:(NSURLProtectionSpace *)space;

@end

2. NSURLCredentialStorage分類(lèi)NSURLSessionTaskAdditions

@interface NSURLCredentialStorage (NSURLSessionTaskAdditions)

- (void)getCredentialsForProtectionSpace:(NSURLProtectionSpace *)protectionSpace task:(NSURLSessionTask *)task completionHandler:(void (^) (NSDictionary<NSString *, NSURLCredential *> * _Nullable credentials))completionHandler API_AVAILABLE(macos(10.10), ios(8.0), watchos(2.0), tvos(9.0));
- (void)setCredential:(NSURLCredential *)credential forProtectionSpace:(NSURLProtectionSpace *)protectionSpace task:(NSURLSessionTask *)task API_AVAILABLE(macos(10.10), ios(8.0), watchos(2.0), tvos(9.0));
- (void)removeCredential:(NSURLCredential *)credential forProtectionSpace:(NSURLProtectionSpace *)protectionSpace options:(nullable NSDictionary<NSString *, id> *)options task:(NSURLSessionTask *)task API_AVAILABLE(macos(10.10), ios(8.0), watchos(2.0), tvos(9.0));
- (void)getDefaultCredentialForProtectionSpace:(NSURLProtectionSpace *)space task:(NSURLSessionTask *)task completionHandler:(void (^) (NSURLCredential * _Nullable credential))completionHandler API_AVAILABLE(macos(10.10), ios(8.0), watchos(2.0), tvos(9.0));
- (void)setDefaultCredential:(NSURLCredential *)credential forProtectionSpace:(NSURLProtectionSpace *)protectionSpace task:(NSURLSessionTask *)task API_AVAILABLE(macos(10.10), ios(8.0), watchos(2.0), tvos(9.0));

@end

后記

本篇主要詳細(xì)解析了類(lèi)NSURLCredentialStorage,喜歡的給個(gè)贊和關(guān)注,謝謝~~~

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容