版本記錄
版本號(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)證钝满。
- 創(chuàng)建并返回
-
+ 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
-
NSURLCredentialPersistence
- 這些常量指定了憑證的保存期限。
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)注庵芭,謝謝~~~