詳細(xì)解析幾個(gè)和網(wǎng)絡(luò)請(qǐng)求有關(guān)的類(二十) —— 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è)類。感興趣的可以看上面幾篇文章。
1. 詳細(xì)解析幾個(gè)和網(wǎng)絡(luò)請(qǐng)求有關(guān)的類 (一) —— NSURLSession
2. 詳細(xì)解析幾個(gè)和網(wǎng)絡(luò)請(qǐng)求有關(guān)的類(二) —— NSURLRequest和NSMutableURLRequest
3. 詳細(xì)解析幾個(gè)和網(wǎng)絡(luò)請(qǐng)求有關(guān)的類(三) —— NSURLConnection
4. 詳細(xì)解析幾個(gè)和網(wǎng)絡(luò)請(qǐng)求有關(guān)的類(四) —— NSURLSession和NSURLConnection的區(qū)別
5. 詳細(xì)解析幾個(gè)和網(wǎng)絡(luò)請(qǐng)求有關(guān)的類(五) —— 關(guān)于NSURL加載系統(tǒng)(一)
6. 詳細(xì)解析幾個(gè)和網(wǎng)絡(luò)請(qǐng)求有關(guān)的類(六) —— 使用NSURLSession(二)
7. 詳細(xì)解析幾個(gè)和網(wǎng)絡(luò)請(qǐng)求有關(guān)的類(七) —— URL數(shù)據(jù)的編碼和解碼(三)
8. 詳細(xì)解析幾個(gè)和網(wǎng)絡(luò)請(qǐng)求有關(guān)的類(八) —— 處理重定向和其他請(qǐng)求更改(四)
9. 詳細(xì)解析幾個(gè)和網(wǎng)絡(luò)請(qǐng)求有關(guān)的類(九) —— 身份驗(yàn)證挑戰(zhàn)和TLS鏈驗(yàn)證(五)
10. 詳細(xì)解析幾個(gè)和網(wǎng)絡(luò)請(qǐng)求有關(guān)的類(十) —— 理解獲取緩存(六)
11. 詳細(xì)解析幾個(gè)和網(wǎng)絡(luò)請(qǐng)求有關(guān)的類(十一) —— Cookies和自定義協(xié)議(七)
12. 詳細(xì)解析幾個(gè)和網(wǎng)絡(luò)請(qǐng)求有關(guān)的類(十二) —— URL Session的生命周期(八)
13. 詳細(xì)解析幾個(gè)和網(wǎng)絡(luò)請(qǐng)求有關(guān)的類(十三) —— NSURLResponse(一)
14. 詳細(xì)解析幾個(gè)和網(wǎng)絡(luò)請(qǐng)求有關(guān)的類(十四) —— NSHTTPCookie(一)
15. 詳細(xì)解析幾個(gè)和網(wǎng)絡(luò)請(qǐng)求有關(guān)的類(十五) —— NSHTTPCookieStorage(一)
16. 詳細(xì)解析幾個(gè)和網(wǎng)絡(luò)請(qǐng)求有關(guān)的類(十六) —— NSURLCache(一)
17. 詳細(xì)解析幾個(gè)和網(wǎng)絡(luò)請(qǐng)求有關(guān)的類(十七) —— NSCachedURLResponse(一)
18. 詳細(xì)解析幾個(gè)和網(wǎng)絡(luò)請(qǐng)求有關(guān)的類(十八) —— NSURLAuthenticationChallenge(一)
19. 詳細(xì)解析幾個(gè)和網(wǎng)絡(luò)請(qǐng)求有關(guān)的類(十九) —— NSURLProtectionSpace(一)

回顧

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


Overview

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

首先看一下該類的基本信息。

URL加載系統(tǒng)支持三種類型的憑證:基于密碼的用戶憑證,基于證書(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

下面我們看一下該類的API文檔。

1. NSURLCredential本類

/*!
    @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è)類是表示認(rèn)證憑證的不可變對(duì)象。 憑證的實(shí)際類型由下面聲明的類別中調(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分類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.
 */
// 此類別定義可用于創(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分類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.
*/
// 此類別定義可用于創(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分類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ì)解析了類NSURLCredential,喜歡的給個(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)容