版本記錄
| 版本號 | 時(shí)間 |
|---|---|
| V1.0 | 2018.03.16 |
前言
我們做APP發(fā)起網(wǎng)絡(luò)請求,一般都是使用框架,這些框架的底層也都是蘋果的API,接下來幾篇就一起來看一下和網(wǎng)絡(luò)有關(guān)的幾個(gè)類。感興趣的可以看上面幾篇文章。
1. 詳細(xì)解析幾個(gè)和網(wǎng)絡(luò)請求有關(guān)的類 (一) —— NSURLSession
2. 詳細(xì)解析幾個(gè)和網(wǎng)絡(luò)請求有關(guān)的類(二) —— NSURLRequest和NSMutableURLRequest
3. 詳細(xì)解析幾個(gè)和網(wǎng)絡(luò)請求有關(guān)的類(三) —— NSURLConnection
4. 詳細(xì)解析幾個(gè)和網(wǎng)絡(luò)請求有關(guān)的類(四) —— NSURLSession和NSURLConnection的區(qū)別
5. 詳細(xì)解析幾個(gè)和網(wǎng)絡(luò)請求有關(guān)的類(五) —— 關(guān)于NSURL加載系統(tǒng)(一)
6. 詳細(xì)解析幾個(gè)和網(wǎng)絡(luò)請求有關(guān)的類(六) —— 使用NSURLSession(二)
7. 詳細(xì)解析幾個(gè)和網(wǎng)絡(luò)請求有關(guān)的類(七) —— URL數(shù)據(jù)的編碼和解碼(三)
8. 詳細(xì)解析幾個(gè)和網(wǎng)絡(luò)請求有關(guān)的類(八) —— 處理重定向和其他請求更改(四)
9. 詳細(xì)解析幾個(gè)和網(wǎng)絡(luò)請求有關(guān)的類(九) —— 身份驗(yàn)證挑戰(zhàn)和TLS鏈驗(yàn)證(五)
10. 詳細(xì)解析幾個(gè)和網(wǎng)絡(luò)請求有關(guān)的類(十) —— 理解獲取緩存(六)
11. 詳細(xì)解析幾個(gè)和網(wǎng)絡(luò)請求有關(guān)的類(十一) —— Cookies和自定義協(xié)議(七)
12. 詳細(xì)解析幾個(gè)和網(wǎng)絡(luò)請求有關(guān)的類(十二) —— URL Session的生命周期(八)
13. 詳細(xì)解析幾個(gè)和網(wǎng)絡(luò)請求有關(guān)的類(十三) —— NSURLResponse(一)
14. 詳細(xì)解析幾個(gè)和網(wǎng)絡(luò)請求有關(guān)的類(十四) —— NSHTTPCookie(一)
15. 詳細(xì)解析幾個(gè)和網(wǎng)絡(luò)請求有關(guān)的類(十五) —— NSHTTPCookieStorage(一)
16. 詳細(xì)解析幾個(gè)和網(wǎng)絡(luò)請求有關(guān)的類(十六) —— NSURLCache(一)
回顧
上一篇講述了NSURLCache這個(gè)類的詳細(xì)信息以及一些注意要點(diǎn),下面這篇我們就主要看一下NSCachedURLResponse。
Overview
對URL請求的緩存響應(yīng)。
首先看一下基本信息

NSCachedURLResponse對象以NSURLResponse對象的形式提供服務(wù)器的響應(yīng)元數(shù)據(jù),以及包含實(shí)際緩存的內(nèi)容數(shù)據(jù)的NSData對象。 其存儲策略決定響應(yīng)是應(yīng)該緩存在磁盤上,內(nèi)存中還是根本不緩存。
緩存的響應(yīng)還包含用戶信息字典,您可以在其中存儲有關(guān)緩存項(xiàng)目的特定于應(yīng)用程序的信息。
NSURLCache類存儲和檢索NSCachedURLResponse的實(shí)例。
Topics
1. Creating a cached URL response - 創(chuàng)建一個(gè)緩存的URL響應(yīng)
-
- initWithResponse:data:
- 初始化一個(gè)
NSCachedURLResponse對象。
- 初始化一個(gè)
-
- initWithResponse:data:userInfo:storagePolicy:`
- 初始化一個(gè)
NSCachedURLResponse對象。
- 初始化一個(gè)
2. Getting cached URL response properties - 獲取緩存的響應(yīng)屬性
-
data
- 接收的緩存數(shù)據(jù)。
-
response
- 與接收者關(guān)聯(lián)的
URL response對象。
- 與接收者關(guān)聯(lián)的
-
storagePolicy
- 接收者的緩存存儲策略。
-
userInfo
- 接受者的info字典。
3. Constants
-
NSURLCacheStoragePolicy
- 這些常量指定NSCachedURLResponse對象使用的緩存策略。
API文檔
下面我們就看一下API文檔。
/*!
@class NSCachedURLResponse
NSCachedURLResponse is a class whose objects functions as a wrapper for
objects that are stored in the framework's caching system.
It is used to maintain characteristics and attributes of a cached
object.
*/
// NSCachedURLResponse是一個(gè)類,其對象充當(dāng)存儲在框架緩存系統(tǒng)中的對象的包裝器。
// 它用于維護(hù)緩存對象的特征和屬性。
@interface NSCachedURLResponse : NSObject <NSSecureCoding, NSCopying>
{
@private
NSCachedURLResponseInternal *_internal;
}
/*!
@method initWithResponse:data
@abstract Initializes an NSCachedURLResponse with the given
response and data.
@discussion A default NSURLCacheStoragePolicy is used for
NSCachedURLResponse objects initialized with this method:
NSURLCacheStorageAllowed.
@param response a NSURLResponse object.
@param data an NSData object representing the URL content
corresponding to the given response.
@result an initialized NSCachedURLResponse.
*/
// 默認(rèn)NSURLCacheStoragePolicy用于通過此方法初始化的NSCachedURLResponse對象:NSURLCacheStorageAllowed
// response:一個(gè)NSURLResponse對象
// data:表示與給定響應(yīng)對應(yīng)的URL內(nèi)容的NSData對象
- (instancetype)initWithResponse:(NSURLResponse *)response data:(NSData *)data;
/*!
@method initWithResponse:data:userInfo:storagePolicy:
@abstract Initializes an NSCachedURLResponse with the given
response, data, user-info dictionary, and storage policy.
@param response a NSURLResponse object.
@param data an NSData object representing the URL content
corresponding to the given response.
@param userInfo a dictionary user-specified information to be
stored with the NSCachedURLResponse.
@param storagePolicy an NSURLCacheStoragePolicy constant.
@result an initialized NSCachedURLResponse.
*/
// 根據(jù)一個(gè)給定的response、data、user-info字典和存儲策略初始化一個(gè)NSCachedURLResponse對象。
// userInfo: 一個(gè)字典用戶指定的信息與NSCachedURLResponse一起存儲
- (instancetype)initWithResponse:(NSURLResponse *)response data:(NSData *)data userInfo:(nullable NSDictionary *)userInfo storagePolicy:(NSURLCacheStoragePolicy)storagePolicy;
/*!
@abstract Returns the response wrapped by this instance.
@result The response wrapped by this instance.
*/
// 返回由此實(shí)例包裝的響應(yīng)
@property (readonly, copy) NSURLResponse *response;
/*!
@abstract Returns the data of the receiver.
@result The data of the receiver.
*/
// 返回接收器的數(shù)據(jù)
@property (readonly, copy) NSData *data;
/*!
@abstract Returns the userInfo dictionary of the receiver.
@result The userInfo dictionary of the receiver.
*/
// 返回接收者的userInfo字典
@property (nullable, readonly, copy) NSDictionary *userInfo;
/*!
@abstract Returns the NSURLCacheStoragePolicy constant of the receiver.
@result The NSURLCacheStoragePolicy constant of the receiver.
*/
// 返回接收器的NSURLCacheStoragePolicy常量
@property (readonly) NSURLCacheStoragePolicy storagePolicy;
@end
NSURLRequest這里和NSCachedURLResponse是對應(yīng)的關(guān)系。通過這樣的映射關(guān)系實(shí)現(xiàn)緩存。這里存的是NSCachedURLResponse,也就是說這里的NSURLCache也是一個(gè)類似于NSCache的容器,只不過data是NSCachedURLResponse對象. 并不是類似于image這樣的data。
后記
本篇主要講述
NSCachedURLResponse類的基本情況和使用。
