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

版本記錄

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

前言

我們做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(一)

回顧

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


Overview

身份驗(yàn)證憑據(jù)滋戳,包含特定于憑證類(lèi)型的身份驗(yàn)證信息以及要使用的持久性存儲(chǔ)的類(lèi)型(如果有)。

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

URL加載系統(tǒng)支持三種類(lèi)型的憑證:基于密碼的用戶憑證胧瓜,基于證書(shū)的用戶憑證和基于證書(shū)的服務(wù)器憑證(用于驗(yàn)證服務(wù)器身份時(shí)使用)。

當(dāng)您創(chuàng)建憑證時(shí)郑什,您可以指定它應(yīng)該用于單個(gè)請(qǐng)求府喳,暫時(shí)保留(直到您的應(yīng)用程序退出)或永久保存(在鑰匙串中)。


Topics

1. Creating a credential - 創(chuàng)建憑證

  • + credentialForTrust:
    • 創(chuàng)建并返回NSURLCredential對(duì)象蘑拯,以使用給定的接受信任進(jìn)行服務(wù)器信任認(rèn)證钝满。
  • + credentialWithUser:password:persistence:
    • 使用給定的持久性設(shè)置創(chuàng)建并返回一個(gè)NSURLCredential對(duì)象,用于使用給定的用戶名和密碼進(jìn)行Internet密碼驗(yàn)證申窘。
  • + credentialWithIdentity:certificates:persistence:
    • 使用給定的持久性設(shè)置創(chuàng)建并返回一個(gè)NSURLCredential對(duì)象弯蚜,用于使用給定身份和給定客戶端證書(shū)數(shù)組進(jìn)行客戶端證書(shū)身份驗(yàn)證。
  • - initWithIdentity:certificates:persistence:
    • 返回客戶端證書(shū)身份驗(yàn)證的NSURLCredential對(duì)象剃法,該對(duì)象使用給定的持久性設(shè)置使用給定的身份和給定的客戶端證書(shū)數(shù)組進(jìn)行初始化碎捺。
  • - initWithTrust:
    • 返回一個(gè)NSURLCredential對(duì)象,用于使用給定的接受信任進(jìn)行初始化的服務(wù)器信任認(rèn)證贷洲。
  • - initWithUser:password:persistence:
    • 使用給定的持久性設(shè)置返回使用給定用戶名和密碼初始化的NSURLCredential對(duì)象收厨。

2. Getting credential properties - 獲取憑證屬性

  • user
    • 接收者的用戶名。
  • certificates
    • 憑據(jù)的證書(shū)优构,如果它是客戶端證書(shū)憑據(jù)诵叁。
  • hasPassword
    • 一個(gè)布爾值,指示接收者是否有密碼钦椭。
  • password
    • 接收者密碼
  • identity
    • 此憑證的身份(如果它是客戶證書(shū)憑證)拧额。
  • persistence
    • 接受者的持久化設(shè)置碑诉。

3. Constants


API

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

1. NSURLCredential本類(lèi)

/*!
    @class NSURLCredential
    @discussion This class is an immutable object representing an authentication credential.  The actual type of the credential is determined by the constructor called in the categories declared below.
*/
// 這個(gè)類(lèi)是表示認(rèn)證憑證的不可變對(duì)象进栽。 憑證的實(shí)際類(lèi)型由下面聲明的類(lèi)別中調(diào)用的構(gòu)造函數(shù)確定。

@interface NSURLCredential : NSObject <NSSecureCoding, NSCopying>
{
    @private
    __strong NSURLCredentialInternal *_internal;
}

/*!
    @abstract Determine whether this credential is or should be stored persistently
    @result A value indicating whether this credential is stored permanently, per session or not at all.
 */
// 確定此憑證是否應(yīng)持久存儲(chǔ)或應(yīng)該存儲(chǔ)
// @result 一個(gè)值恭垦,指示此憑證是否是永久存儲(chǔ)快毛,每個(gè)會(huì)話還是根本不存儲(chǔ)
// typedef NS_ENUM(NSUInteger, NSURLCredentialPersistence) {
    NSURLCredentialPersistenceNone,
    NSURLCredentialPersistenceForSession,
    NSURLCredentialPersistencePermanent,
    NSURLCredentialPersistenceSynchronizable API_AVAILABLE(macos(10.8), ios(6.0), watchos(2.0), tvos(9.0))
// };

@property (readonly) NSURLCredentialPersistence persistence;

@end

2. NSURLCredential分類(lèi)NSInternetPassword

/*!
    @discussion This category defines the methods available to an NSURLCredential created to represent an internet password credential.  These are most commonly used for resources that require a username and password combination.
 */
// 此類(lèi)別定義可用于創(chuàng)建用于表示Internet密碼憑據(jù)的NSURLCredential的方法。 
// 這些通常用于需要用戶名和密碼組合的資源署照。

@interface NSURLCredential(NSInternetPassword)

/*!
    @method initWithUser:password:persistence:
    @abstract Initialize a NSURLCredential with a user and password
    @param user the username
    @param password the password
    @param persistence enum that says to store per session, permanently or not at all
    @result The initialized NSURLCredential
*/
// 使用用戶名和密碼實(shí)例化一個(gè)NSURLCredential對(duì)象。

- (instancetype)initWithUser:(NSString *)user password:(NSString *)password persistence:(NSURLCredentialPersistence)persistence;

/*!
    @method credentialWithUser:password:persistence:
    @abstract Create a new NSURLCredential with a user and password
    @param user the username
    @param password the password
    @param persistence enum that says to store per session, permanently or not at all
    @result The new autoreleased NSURLCredential
*/
// 使用用戶名和密碼實(shí)例化一個(gè)NSURLCredential對(duì)象吗浩。

+ (NSURLCredential *)credentialWithUser:(NSString *)user password:(NSString *)password persistence:(NSURLCredentialPersistence)persistence;

/*!
    @abstract Get the username
    @result The user string
*/
// 獲取用戶名

@property (nullable, readonly, copy) NSString *user;

/*!
    @abstract Get the password
    @result The password string
    @discussion This method might actually attempt to retrieve the
    password from an external store, possible resulting in prompting,
    so do not call it unless needed.
*/
// 獲取密碼建芙,此方法實(shí)際上可能試圖從外部存儲(chǔ)中檢索密碼,可能導(dǎo)致提示懂扼,因此除非需要禁荸,否則不要調(diào)用它。

@property (nullable, readonly, copy) NSString *password;

/*!
    @abstract Find out if this credential has a password, without trying to get it
    @result YES if this credential has a password, otherwise NO
    @discussion If this credential's password is actually kept in an
    external store, the password method may return nil even if this
    method returns YES, since getting the password may fail, or the
    user may refuse access.
*/
// 找出這個(gè)憑證是否有密碼阀湿,如果此憑證的密碼實(shí)際上保存在外部存儲(chǔ)中赶熟,
// 如果此憑證的密碼實(shí)際上保存在外部存儲(chǔ)中,那么即使此方法返回YES陷嘴,
// 密碼方法也可能返回nil映砖,因?yàn)楂@取密碼可能會(huì)失敗,或者用戶可能拒絕訪問(wèn)灾挨。

@property (readonly) BOOL hasPassword;

@end

3. NSURLCredential分類(lèi)NSClientCertificate

/*!
    @discussion This category defines the methods available to an NSURLCredential created to represent a client certificate credential.  Client certificates are commonly stored on the users computer in the keychain and must be presented to the server during a handshake.
*/
// 此類(lèi)別定義可用于創(chuàng)建用于表示客戶端證書(shū)憑證的NSURLCredential的方法邑退。
// 客戶端證書(shū)通常存儲(chǔ)在鑰匙串中的用戶計(jì)算機(jī)上,并且在握手期間必須呈現(xiàn)給服務(wù)器劳澄。

@interface NSURLCredential(NSClientCertificate)

/*!
    @method initWithIdentity:certificates:persistence:
    @abstract Initialize an NSURLCredential with an identity and array of at least 1 client certificates (SecCertificateRef)
    @param identity a SecIdentityRef object
    @param certArray an array containing at least one SecCertificateRef objects
    @param persistence enum that says to store per session, permanently or not at all
    @result the Initialized NSURLCredential
 */
// 使用至少一個(gè)客戶端證書(shū)(SecCertificateRef)的標(biāo)識(shí)和數(shù)組初始化NSURLCredential

- (instancetype)initWithIdentity:(SecIdentityRef)identity certificates:(nullable NSArray *)certArray persistence:(NSURLCredentialPersistence)persistence API_AVAILABLE(macos(10.6), ios(3.0), watchos(2.0), tvos(9.0));

/*!
    @method credentialWithIdentity:certificates:persistence:
    @abstract Create a new NSURLCredential with an identity and certificate array
    @param identity a SecIdentityRef object
    @param certArray an array containing at least one SecCertificateRef objects
    @param persistence enum that says to store per session, permanently or not at all
    @result The new autoreleased NSURLCredential
 */
// 用身份證書(shū)和證書(shū)數(shù)組創(chuàng)建一個(gè)新的NSURLCredential

+ (NSURLCredential *)credentialWithIdentity:(SecIdentityRef)identity certificates:(nullable NSArray *)certArray persistence:(NSURLCredentialPersistence)persistence API_AVAILABLE(macos(10.6), ios(3.0), watchos(2.0), tvos(9.0));

/*!
    @abstract Returns the SecIdentityRef of this credential, if it was created with a certificate and identity
    @result A SecIdentityRef or NULL if this is a username/password credential
 */
// 返回此憑據(jù)的SecIdentityRef(如果它是使用證書(shū)和身份創(chuàng)建的)
// @result 如果這是用戶名/密碼憑證地技,則為SecIdentityRef或NULL

@property (nullable, readonly) SecIdentityRef identity;

/*!
    @abstract Returns an NSArray of SecCertificateRef objects representing the client certificate for this credential, if this credential was created with an identity and certificate.
    @result an NSArray of SecCertificateRef or NULL if this is a username/password credential
 */
// 如果此憑證是使用標(biāo)識(shí)和證書(shū)創(chuàng)建的,則返回表示此憑證的客戶端證書(shū)的SecCertificateRef對(duì)象的NSArray秒拔。
// @result 如果這是一個(gè)用戶名/密碼憑證莫矗,返回SecCertificateRef的一個(gè)NSArray或者NULL

@property (readonly, copy) NSArray *certificates API_AVAILABLE(macos(10.6), ios(3.0), watchos(2.0), tvos(9.0));

@end

4. NSURLCredential分類(lèi)NSServerTrust

@interface NSURLCredential(NSServerTrust)

/*!
    @method initWithTrust:
    @abstract Initialize a new NSURLCredential which specifies that the specified trust has been accepted.
    @result the Initialized NSURLCredential
 */
// 初始化一個(gè)新的NSURLCredential,它指定指定的信任已被接受砂缩。

- (instancetype)initWithTrust:(SecTrustRef)trust API_AVAILABLE(macos(10.6), ios(3.0), watchos(2.0), tvos(9.0));

/*!
    @method credentialForTrust:
    @abstract Create a new NSURLCredential which specifies that a handshake has been trusted.
    @result The new autoreleased NSURLCredential
 */
// 創(chuàng)建一個(gè)指定握手已被信任的新NSURLCredential作谚。

+ (NSURLCredential *)credentialForTrust:(SecTrustRef)trust API_AVAILABLE(macos(10.6), ios(3.0), watchos(2.0), tvos(9.0));

@end

后記

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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末食磕,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子喳挑,更是在濱河造成了極大的恐慌彬伦,老刑警劉巖滔悉,帶你破解...
    沈念sama閱讀 217,185評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異单绑,居然都是意外死亡回官,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門(mén)搂橙,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)歉提,“玉大人,你說(shuō)我怎么就攤上這事区转√蓿” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,524評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵废离,是天一觀的道長(zhǎng)侄泽。 經(jīng)常有香客問(wèn)我,道長(zhǎng)蜻韭,這世上最難降的妖魔是什么悼尾? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,339評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮肖方,結(jié)果婚禮上闺魏,老公的妹妹穿的比我還像新娘。我一直安慰自己俯画,他們只是感情好析桥,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,387評(píng)論 6 391
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著艰垂,像睡著了一般烹骨。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上材泄,一...
    開(kāi)封第一講書(shū)人閱讀 51,287評(píng)論 1 301
  • 那天沮焕,我揣著相機(jī)與錄音,去河邊找鬼拉宗。 笑死峦树,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的旦事。 我是一名探鬼主播魁巩,決...
    沈念sama閱讀 40,130評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼姐浮!你這毒婦竟也來(lái)了谷遂?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 38,985評(píng)論 0 275
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤卖鲤,失蹤者是張志新(化名)和其女友劉穎肾扰,沒(méi)想到半個(gè)月后畴嘶,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,420評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡集晚,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,617評(píng)論 3 334
  • 正文 我和宋清朗相戀三年窗悯,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片偷拔。...
    茶點(diǎn)故事閱讀 39,779評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡蒋院,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出莲绰,到底是詐尸還是另有隱情欺旧,我是刑警寧澤,帶...
    沈念sama閱讀 35,477評(píng)論 5 345
  • 正文 年R本政府宣布蛤签,位于F島的核電站辞友,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏顷啼。R本人自食惡果不足惜踏枣,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,088評(píng)論 3 328
  • 文/蒙蒙 一昌屉、第九天 我趴在偏房一處隱蔽的房頂上張望钙蒙。 院中可真熱鬧,春花似錦间驮、人聲如沸躬厌。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,716評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)扛施。三九已至,卻和暖如春屹篓,著一層夾襖步出監(jiān)牢的瞬間疙渣,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,857評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工堆巧, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留妄荔,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,876評(píng)論 2 370
  • 正文 我出身青樓谍肤,卻偏偏與公主長(zhǎng)得像啦租,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子荒揣,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,700評(píng)論 2 354

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