好文章收藏夹

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

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

版本记录

版本号 时间
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(一)

回顾

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


Overview

服务器或服务器上的区域,通常称为领域,需要身份验证。

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

保护空间定义了一系列匹配约束条件,用于确定应提供哪种凭证。 例如,如果一个请求提供了请求客户端用户名和密码的NSURLAuthenticationChallenge对象,则应用程序应为挑战的保护空间中指定的特定主机,端口,协议和领域提供正确的用户名和密码。

注意:这个类没有指定的初始化程序; 它的init方法总是返回nil。 您必须通过调用Creating a protection space中描述的其中一种初始化方法来初始化此类。


Topics

1. Creating a protection space - 创建一个保护空间

2. Getting protection space properties - 获取保护空间的属性

3. Constants


API

下面我们看一下苹果的API文档,下面我就贴出来并添加注释。

1. NSURLProtectionSpace本类

NS_ASSUME_NONNULL_BEGIN

/*!
   @const NSURLProtectionSpaceHTTP
   @abstract The protocol for HTTP
*/
// HTTP协议

FOUNDATION_EXPORT NSString * const NSURLProtectionSpaceHTTP API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));


/*!
   @const NSURLProtectionSpaceHTTPS
   @abstract The protocol for HTTPS
*/
// HTTPS协议

FOUNDATION_EXPORT NSString * const NSURLProtectionSpaceHTTPS API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));


/*!
   @const NSURLProtectionSpaceFTP
   @abstract The protocol for FTP
*/
// FTP协议

FOUNDATION_EXPORT NSString * const NSURLProtectionSpaceFTP API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));

/*!
    @const NSURLProtectionSpaceHTTPProxy
    @abstract The proxy type for http proxies
*/
// HTTP代理的代理类型

FOUNDATION_EXPORT NSString * const NSURLProtectionSpaceHTTPProxy;

/*!
    @const NSURLProtectionSpaceHTTPSProxy
    @abstract The proxy type for https proxies
*/
// HTTPS代理的代理类型

FOUNDATION_EXPORT NSString * const NSURLProtectionSpaceHTTPSProxy;

/*!
    @const NSURLProtectionSpaceFTPProxy
    @abstract The proxy type for ftp proxies
*/
// FTP代理的代理类型

FOUNDATION_EXPORT NSString * const NSURLProtectionSpaceFTPProxy;

/*!
    @const NSURLProtectionSpaceSOCKSProxy
    @abstract The proxy type for SOCKS proxies
*/
// SOCKS代理的代理类型

FOUNDATION_EXPORT NSString * const NSURLProtectionSpaceSOCKSProxy;

/*!
    @const NSURLAuthenticationMethodDefault
    @abstract The default authentication method for a protocol
*/
// 协议的默认验证方法

FOUNDATION_EXPORT NSString * const NSURLAuthenticationMethodDefault;

/*!
    @const NSURLAuthenticationMethodHTTPBasic
    @abstract HTTP basic authentication. Equivalent to
    NSURLAuthenticationMethodDefault for http.
*/
// HTTP基本验证,和HTTP的NSURLAuthenticationMethodDefault是一样的

FOUNDATION_EXPORT NSString * const NSURLAuthenticationMethodHTTPBasic;

/*!
    @const NSURLAuthenticationMethodHTTPDigest
    @abstract HTTP digest authentication.
*/
// HTTP摘要认证

FOUNDATION_EXPORT NSString * const NSURLAuthenticationMethodHTTPDigest;

/*!
    @const NSURLAuthenticationMethodHTMLForm
    @abstract HTML form authentication. Applies to any protocol.
*/
// HTML表单认证。 适用于任何协议

FOUNDATION_EXPORT NSString * const NSURLAuthenticationMethodHTMLForm;

/*!
   @const NSURLAuthenticationMethodNTLM
   @abstract NTLM authentication.
*/
// NTLM验证

FOUNDATION_EXPORT NSString * const NSURLAuthenticationMethodNTLM API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));

/*!
   @const NSURLAuthenticationMethodNegotiate
   @abstract Negotiate authentication.
*/
// 协商身份验证

FOUNDATION_EXPORT NSString * const NSURLAuthenticationMethodNegotiate API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));

/*!
    @const NSURLAuthenticationMethodClientCertificate
    @abstract SSL Client certificate.  Applies to any protocol.
 */
// SSL客户端证书。 适用于任何协议

FOUNDATION_EXPORT NSString * const NSURLAuthenticationMethodClientCertificate API_AVAILABLE(macos(10.6), ios(3.0), watchos(2.0), tvos(9.0));

/*!
    @const NSURLAuthenticationMethodServerTrust
    @abstract SecTrustRef validation required.  Applies to any protocol.
 */
// 需要SecTrustRef验证。 适用于任何协议

FOUNDATION_EXPORT NSString * const NSURLAuthenticationMethodServerTrust API_AVAILABLE(macos(10.6), ios(3.0), watchos(2.0), tvos(9.0));

@class NSURLProtectionSpaceInternal;

/*!
    @class NSURLProtectionSpace
    @discussion This class represents a protection space requiring authentication.
*/

@interface NSURLProtectionSpace : NSObject <NSSecureCoding, NSCopying>
{
@private
    NSURLProtectionSpaceInternal *_internal;
}

/*!
    @method initWithHost:port:protocol:realm:authenticationMethod:
    @abstract Initialize a protection space representing an origin server, or a realm on one
    @param host The hostname of the server
    @param port The port for the server
    @param protocol The sprotocol for this server - e.g. "http", "ftp", "https"
    @param realm A string indicating a protocol-specific subdivision
    of a single host. For http and https, this maps to the realm
    string in http authentication challenges. For many other protocols
    it is unused.
    @param authenticationMethod The authentication method to use to access this protection space -
    valid values include nil (default method), @"digest" and @"form".
    @result The initialized object.
*/
// @abstract 初始化代表原始服务器的保护空间或一个域
// @param realm 指示单个主机的协议特定细分的字符串。 对于http和https,
// 这将映射到http认证挑战中的领域字符串。 对于许多其他协议,它未被使用。
// @param authenticationMethod 用于访问此保护空间的身份验证方法 - 有效值包括nil(默认方法),@“digest”和@“form”

- (instancetype)initWithHost:(NSString *)host port:(NSInteger)port protocol:(nullable NSString *)protocol realm:(nullable NSString *)realm authenticationMethod:(nullable NSString *)authenticationMethod;

/*!
    @method initWithProxyHost:port:type:realm:authenticationMethod:
    @abstract Initialize a protection space representing a proxy server, or a realm on one
    @param host The hostname of the proxy server
    @param port The port for the proxy server
    @param type The type of proxy - e.g. "http", "ftp", "SOCKS"
    @param realm A string indicating a protocol-specific subdivision
    of a single host. For http and https, this maps to the realm
    string in http authentication challenges. For many other protocols
    it is unused.
    @param authenticationMethod The authentication method to use to access this protection space -
    valid values include nil (default method) and @"digest"
    @result The initialized object.
*/
// 这个也是初始化的一个方法

- (instancetype)initWithProxyHost:(NSString *)host port:(NSInteger)port type:(nullable NSString *)type realm:(nullable NSString *)realm  authenticationMethod:(nullable NSString *)authenticationMethod;

/*!
    @abstract Get the authentication realm for which the protection space that
    needs authentication
    @discussion This is generally only available for http
    authentication, and may be nil otherwise.
    @result The realm string
*/
// 获取需要身份验证的保护空间的身份验证领域。
// 这通常只适用于http身份验证,并且可能是nil
// 返回值就是域字符串

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

/*!
    @abstract Determine if the password for this protection space can be sent securely
    @result YES if a secure authentication method or protocol will be used, NO otherwise
*/
// 确定是否可以安全地发送此保护空间的密码
// YES,如果安全验证方法或协议将被使用,否则就是NO

@property (readonly) BOOL receivesCredentialSecurely;

/*!
    @abstract Determine if this authenticating protection space is a proxy server
    @result YES if a proxy, NO otherwise
*/
// 确定此身份验证保护空间是否是代理服务器,YES代表就是代理服务器,否则为NO

@property (readonly) BOOL isProxy;

/*!
    @abstract Get the proxy host if this is a proxy authentication, or the host from the URL.
    @result The host for this protection space.
*/
// 如果这是代理身份验证,或者来自URL的主机,就获取代理主机

@property (readonly, copy) NSString *host;

/*!
    @abstract Get the proxy port if this is a proxy authentication, or the port from the URL.
    @result The port for this protection space, or 0 if not set.
*/
// 如果这是代理身份验证,或者来自URL的端口,就获取代理端口,返回结果就是保护空间的端口,如果没有设置就返回0

@property (readonly) NSInteger port;

/*!
    @abstract Get the type of this protection space, if a proxy
    @result The type string, or nil if not a proxy.
 */
// 如果是代理,就获取这个保护空间的类型。返回结果就是类型字符串,如果不是代理就返回nil

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

/*!
    @abstract Get the protocol of this protection space, if not a proxy
    @result The type string, or nil if a proxy.
*/
// 如果不设置代理,获取保护空间的协议。返回结果就是类型字符串,如果不是代理就返回nil

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

/*!
    @abstract Get the authentication method to be used for this protection space
    @result The authentication method
*/
// 获取这个保护空间要使用的身份验证方法

@property (readonly, copy) NSString *authenticationMethod;

@end

2. NSURLProtectionSpace分类NSClientCertificateSpace

/*!
    @discussion This category supplies additional information for use when a client certificate is required by the server in order to complete authentication.
 */
// 当服务器需要客户端证书以完成身份验证时,此类别提供附加信息以供使用。
@interface NSURLProtectionSpace(NSClientCertificateSpace)

/*!
    @abstract Returns an array of acceptable certificate issuing authorities for client certification authentication. Issuers are identified by their distinguished name and returned as a DER encoded data.
    @result An array of NSData objects.  (Nil if the authenticationMethod is not NSURLAuthenticationMethodClientCertificate)
 */
// @abstract 返回用于客户端认证认证的可接受证书颁发机构的数组。 发行人以其专有名称标识并作为DER编码数据返回。
// @result 一个NSData对象数组。 (如果authenticationMethod不是NSURLAuthenticationMethodClientCertificate则为nil)

@property (nullable, readonly, copy) NSArray<NSData *> *distinguishedNames API_AVAILABLE(macos(10.6), ios(3.0), watchos(2.0), tvos(9.0));

@end

3. NSURLProtectionSpace分类NSServerTrustValidationSpace

/*!
    @discussion This category supplies additional information for use by the client to evaluate whether to trust a given server during a security handshake.
 */
// 此类别提供了供客户端用于评估在安全握手期间是否信任给定服务器的附加信息

@interface NSURLProtectionSpace(NSServerTrustValidationSpace)

/*!
    @abstract Returns a SecTrustRef which represents the state of the servers SSL transaction state
    @result A SecTrustRef from Security.framework.  (Nil if the authenticationMethod is not NSURLAuthenticationMethodServerTrust)
 */
// 返回表示服务器SSL事务状态状态的SecTrustRef
// 来自Security.framework的SecTrustRef。 (如果authenticationMethod不是NSURLAuthenticationMethodServerTrust则为零)

@property (nullable, readonly) SecTrustRef serverTrust API_AVAILABLE(macos(10.6), ios(3.0), watchos(2.0), tvos(9.0));

@end

后记

本篇主要讲述的就是NSURLProtectionSpace类的详细信息,喜欢的给个关注和赞,谢谢~~~

上一篇下一篇

猜你喜欢

热点阅读