好文章收藏夹

详细解析几个和网络请求有关的类(二十) —— NSURLCred

2018-03-17  本文已影响14人  刀客传奇

版本记录

版本号 时间
V1.0 2018.03.17

前言

我们做APP发起网络请求,一般都是使用框架,这些框架的底层也都是苹果的API,接下来几篇就一起来看一下和网络有关的几个类。感兴趣的可以看上面几篇文章。
1. 详细解析几个和网络请求有关的类 (一) —— NSURLSession
2. 详细解析几个和网络请求有关的类(二) —— NSURLRequest和NSMutableURLRequest
3. 详细解析几个和网络请求有关的类(三) —— NSURLConnection
4. 详细解析几个和网络请求有关的类(四) —— NSURLSession和NSURLConnection的区别
5. 详细解析几个和网络请求有关的类(五) —— 关于NSURL加载系统(一)
6. 详细解析几个和网络请求有关的类(六) —— 使用NSURLSession(二)
7. 详细解析几个和网络请求有关的类(七) —— URL数据的编码和解码(三)
8. 详细解析几个和网络请求有关的类(八) —— 处理重定向和其他请求更改(四)
9. 详细解析几个和网络请求有关的类(九) —— 身份验证挑战和TLS链验证(五)
10. 详细解析几个和网络请求有关的类(十) —— 理解获取缓存(六)
11. 详细解析几个和网络请求有关的类(十一) —— Cookies和自定义协议(七)
12. 详细解析几个和网络请求有关的类(十二) —— URL Session的生命周期(八)
13. 详细解析几个和网络请求有关的类(十三) —— NSURLResponse(一)
14. 详细解析几个和网络请求有关的类(十四) —— NSHTTPCookie(一)
15. 详细解析几个和网络请求有关的类(十五) —— NSHTTPCookieStorage(一)
16. 详细解析几个和网络请求有关的类(十六) —— NSURLCache(一)
17. 详细解析几个和网络请求有关的类(十七) —— NSCachedURLResponse(一)
18. 详细解析几个和网络请求有关的类(十八) —— NSURLAuthenticationChallenge(一)
19. 详细解析几个和网络请求有关的类(十九) —— NSURLProtectionSpace(一)

回顾

上一篇讲述了NSURLProtectionSpace这个类的详细信息以及一些注意要点,下面这篇我们就主要看一下NSURLCredential


Overview

身份验证凭据,包含特定于凭证类型的身份验证信息以及要使用的持久性存储的类型(如果有)。

首先看一下该类的基本信息。

URL加载系统支持三种类型的凭证:基于密码的用户凭证,基于证书的用户凭证和基于证书的服务器凭证(用于验证服务器身份时使用)。

当您创建凭证时,您可以指定它应该用于单个请求,暂时保留(直到您的应用程序退出)或永久保存(在钥匙串中)。


Topics

1. Creating a credential - 创建凭证

2. Getting credential properties - 获取凭证属性

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.
*/
// 这个类是表示认证凭证的不可变对象。 凭证的实际类型由下面声明的类别中调用的构造函数确定。

@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.
 */
// 确定此凭证是否应持久存储或应该存储
// @result 一个值,指示此凭证是否是永久存储,每个会话还是根本不存储
// 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.
 */
// 此类别定义可用于创建用于表示Internet密码凭据的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
*/
// 使用用户名和密码实例化一个NSURLCredential对象。

- (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
*/
// 使用用户名和密码实例化一个NSURLCredential对象。

+ (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.
*/
// 获取密码,此方法实际上可能试图从外部存储中检索密码,可能导致提示,因此除非需要,否则不要调用它。

@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.
*/
// 找出这个凭证是否有密码,如果此凭证的密码实际上保存在外部存储中,
// 如果此凭证的密码实际上保存在外部存储中,那么即使此方法返回YES,
// 密码方法也可能返回nil,因为获取密码可能会失败,或者用户可能拒绝访问。

@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.
*/
// 此类别定义可用于创建用于表示客户端证书凭证的NSURLCredential的方法。
// 客户端证书通常存储在钥匙串中的用户计算机上,并且在握手期间必须呈现给服务器。

@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
 */
// 使用至少一个客户端证书(SecCertificateRef)的标识和数组初始化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
 */
// 用身份证书和证书数组创建一个新的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
 */
// 返回此凭据的SecIdentityRef(如果它是使用证书和身份创建的)
// @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
 */
// 如果此凭证是使用标识和证书创建的,则返回表示此凭证的客户端证书的SecCertificateRef对象的NSArray。
// @result 如果这是一个用户名/密码凭证,返回SecCertificateRef的一个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
 */
// 初始化一个新的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
 */
// 创建一个指定握手已被信任的新NSURLCredential。

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

@end

后记

本篇主要详细解析了类NSURLCredential,喜欢的给个赞和关注,谢谢~~~

上一篇下一篇

猜你喜欢

热点阅读