版本记录
版本号 | 时间 |
---|---|
V1.0 | 2018.03.14 |
前言
我们做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的生命周期(八)
回顾
前一篇讲述了关于NSURLSession的声明周期,这一篇讲述关于NSURLResponse
。
NSURLResponse
首先我们看一下苹果的API文档,里面会添加注释。
/*!
@class NSURLResponse
@abstract An NSURLResponse object represents a URL load response in a
manner independent of protocol and URL scheme.
@discussion NSURLResponse encapsulates the metadata associated
with a URL load. Note that NSURLResponse objects do not contain
the actual bytes representing the content of a URL. See
NSURLConnection and NSURLConnectionDelegate for more information
about receiving the content data for a URL load.
*/
@interface NSURLResponse : NSObject
{
@package
NSURLResponseInternal *_internal;
}
/*!
@method initWithURL:MIMEType:expectedContentLength:textEncodingName:
@abstract Initialize an NSURLResponse with the provided values.
@param URL the URL
@param MIMEType the MIME content type of the response
@param length the expected content length of the associated data
@param name the name of the text encoding for the associated data, if applicable, else nil
@result The initialized NSURLResponse.
@discussion This is the designated initializer for NSURLResponse.
*/
// 这个是NSURLResponse的初始化方法,根据传入的值实例化对象
- (instancetype)initWithURL:(NSURL *)URL MIMEType:(nullable NSString *)MIMEType expectedContentLength:(NSInteger)length textEncodingName:(nullable NSString *)name NS_DESIGNATED_INITIALIZER;
/*!
@abstract Returns the URL of the receiver.
@result The URL of the receiver.
*/
@property (nullable, readonly, copy) NSURL *URL;
/*!
@abstract Returns the MIME type of the receiver.
@discussion The MIME type is based on the information provided
from an origin source. However, that value may be changed or
corrected by a protocol implementation if it can be determined
that the origin server or source reported the information
incorrectly or imprecisely. An attempt to guess the MIME type may
be made if the origin source did not report any such information.
@result The MIME type of the receiver.
*/
// 返回接收者的MIME类型。
// MIME类型基于来源提供的信息。 然而,如果可以确定原始服务器或来源不正确
// 地或不准确地报告信息,那么可以通过协议实施改变或纠正该值。 如果原始来源未
// 报告任何此类信息,则可尝试猜测MIME类型。
@property (nullable, readonly, copy) NSString *MIMEType;
/*!
@abstract Returns the expected content length of the receiver.
@discussion Some protocol implementations report a content length
as part of delivering load metadata, but not all protocols
guarantee the amount of data that will be delivered in actuality.
Hence, this method returns an expected amount. Clients should use
this value as an advisory, and should be prepared to deal with
either more or less data.
@result The expected content length of the receiver, or -1 if
there is no expectation that can be arrived at regarding expected
content length.
*/
// 一些协议实现报告内容长度作为传递加载元数据的一部分,但并非所有
// 协议都能保证实际传递的数据量。 因此,此方法返回预期数量。 客户端
// 应该将此值用作一个参考值,并且应该准备好处理更多或更少的数据的情况。
// @result:接收者的预期内容长度,如果对于预期内容长度没有预期可达到,则为-1。
@property (readonly) long long expectedContentLength;
/*!
@abstract Returns the name of the text encoding of the receiver.
@discussion This name will be the actual string reported by the
origin source during the course of performing a protocol-specific
URL load. Clients can inspect this string and convert it to an
NSStringEncoding or CFStringEncoding using the methods and
functions made available in the appropriate framework.
@result The name of the text encoding of the receiver, or nil if no
text encoding was specified.
*/
// @abstract 返回接收者的文本编码名称。
// @discussion 该名称将是执行特定于协议的URL加载过程中源数据报告的实际字符串。
// 客户端可以检查这个字符串,并使用相应框架中提供的
// 方法和函数将其转换为NSStringEncoding或CFStringEncoding
// @result 接收者文本编码名字,或者如果不指定文本编码名那就返回nil
@property (nullable, readonly, copy) NSString *textEncodingName;
/*!
@abstract Returns a suggested filename if the resource were saved to disk.
@discussion The method first checks if the server has specified a filename using the
content disposition header. If no valid filename is specified using that mechanism,
this method checks the last path component of the URL. If no valid filename can be
obtained using the last path component, this method uses the URL's host as the filename.
If the URL's host can't be converted to a valid filename, the filename "unknown" is used.
In mose cases, this method appends the proper file extension based on the MIME type.
This method always returns a valid filename.
@result A suggested filename to use if saving the resource to disk.
*/
@property (nullable, readonly, copy) NSString *suggestedFilename;
@end
NSURLResponse
对象以独立于协议和URL方案的方式表示URL加载响应。NSURLResponse
封装与URL加载相关联的元数据。 请注意,NSURLResponse
对象不包含表示URL内容的实际字节。 有关接收URL加载的内容数据的更多信息,请参阅NSURLConnection
和NSURLConnectionDelegate
。这里是苹果文档里面进行的描述,现在已经不用NSURLConnection
了,所以应该参考的是NSURLSession
。
NSHTTPURLResponse
首先大家要记住的是这个类继承自NSURLResponse
。
NSHTTPURLResponse
对象表示对HTTP URL加载的响应。 它是NSURLResponse
的一个专门技术,它为访问特定于HTTP协议响应的信息提供了便利。
首先我们看一下该类的API,下面就对API中的方法属性等进行详细的注释。
@class NSHTTPURLResponseInternal;
/*!
@class NSHTTPURLResponse
@abstract An NSHTTPURLResponse object represents a response to an
HTTP URL load. It is a specialization of NSURLResponse which
provides conveniences for accessing information specific to HTTP
protocol responses.
*/
@interface NSHTTPURLResponse : NSURLResponse
{
@package
NSHTTPURLResponseInternal *_httpInternal;
}
/*!
@method initWithURL:statusCode:HTTPVersion:headerFields:
@abstract initializer for NSHTTPURLResponse objects.
@param url the URL from which the response was generated.
@param statusCode an HTTP status code.
@param HTTPVersion The version of the HTTP response as represented by the server. This is typically represented as "HTTP/1.1".
@param headerFields A dictionary representing the header keys and values of the server response.
@result the instance of the object, or NULL if an error occurred during initialization.
@discussion This API was introduced in Mac OS X 10.7.2 and iOS 5.0 and is not available prior to those releases.
*/
// NSHTTPURLResponse对象的初始化
- (nullable instancetype)initWithURL:(NSURL *)url statusCode:(NSInteger)statusCode HTTPVersion:(nullable NSString *)HTTPVersion headerFields:(nullable NSDictionary *)headerFields API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
/*!
@abstract Returns the HTTP status code of the receiver.
@result The HTTP status code of the receiver.
*/
// 返回HTTP状态码
@property (readonly) NSInteger statusCode;
/*!
@abstract Returns a dictionary containing all the HTTP header fields
of the receiver.
@discussion By examining this header dictionary, clients can see
the "raw" header information which was reported to the protocol
implementation by the HTTP server. This may be of use to
sophisticated or special-purpose HTTP clients.
@result A dictionary containing all the HTTP header fields of the
receiver.
*/
// @abstract 返回一个包含所有HTTP头的字典
// @discussion 通过检查此头字典,客户端可以看
// 到由HTTP服务器向协议实现报告的“原始”头信息。
// 这可能对复杂或特殊用途的HTTP客户端有用。
// @result 包含接收器的所有HTTP头字段的字典。
@property (readonly, copy) NSDictionary *allHeaderFields;
/*!
@method localizedStringForStatusCode:
@abstract Convenience method which returns a localized string
corresponding to the status code for this response.
@param statusCode the status code to use to produce a localized string.
@result A localized string corresponding to the given status code.
*/
// 错误码的本地化字符串
+ (NSString *)localizedStringForStatusCode:(NSInteger)statusCode;
@end
NSURLResponseInternal和NSHTTPURLResponseInternal
大家可能注意到API文档中都有这样的二个类NSURLResponseInternal
和NSHTTPURLResponseInternal
。
@interface NSURLResponse : NSObject
{
@package
NSURLResponseInternal *_internal;
}
{
@package
NSHTTPURLResponseInternal *_httpInternal;
}
这个是干什么的呢,说到这里首先我们要说一下几个变量修饰符 - @public 、@protected、@package、@private
。
-
@private
作用范围只能在自身类 ——Methods defined in the class can directly access the instance variables that follow, but subclasses cannot
。 -
@protected
作用范围在自身类和继承自己类 ——Methods defined in the class and any subclasses can directly access the instance variables that follow.This is the default case
。 -
@public
作用范围最大,在任何地方 ——Methods defined in the class and any other classes or modules can di- rectly access the instance variables that follow
。
下面是官方给出的解释
具体详细的解释可以看下面这个表格。
至于这两个类是干啥的,其实我们是用不到的,也不知道苹果为什么要设置这两个类型的实例变量。
后记
本篇主要讲述
NSURLResponse
和NSHTTPURLResponse
。