使用NSURLProtocol实现离线缓存
一、說(shuō)明:
NSURLProtocol可以攔截任何網(wǎng)絡(luò)請(qǐng)求,包含UIWebView中發(fā)出的所有請(qǐng)求。但是在WKWebView中,只能攔截到最初始的請(qǐng)求,內(nèi)嵌的資源下載攔截不到。比如通過(guò)WKWebView加載"http://www.baidu.com",則只能攔截到"http://www.baidu.com",網(wǎng)頁(yè)內(nèi)部的資源加載攔截不到。頁(yè)面跳轉(zhuǎn)屬于最初始請(qǐng)求之內(nèi),可以攔截到。
二、創(chuàng)建NSURLProtocol的子類(lèi),通過(guò)下面的代碼注冊(cè)此協(xié)議類(lèi):
[NSURLProtocol registerClass:[MyURLProtocol class]];
三、下面是此子類(lèi)的代碼:
#import "MyURLProtocol.h"
#define MyURLProtocolHandled @"MyURLProtocolHandled"
//創(chuàng)建archive數(shù)據(jù)模型,重寫(xiě)編碼解碼協(xié)議
@interface MyCacheData : NSObject
@property(nonatomic,strong) NSURLRequest *request;
@property(nonatomic,strong) NSURLResponse *response;
@property(nonatomic,strong) NSData *data;
@end
@interface NSURLRequest (MutableCopyWorkaround)
- (id)mutableCopyWorkaround;
@end
@interface MyURLProtocol ()
@property(nonatomic,strong) NSURLConnection *connection;
@property(nonatomic,strong) NSMutableData *httpData;
@property(nonatomic,strong) NSURLResponse *response;
@end
@implementation MyURLProtocol
#pragma mark - 重寫(xiě)NSURLProtocol子類(lèi)方法
+ (BOOL)canInitWithRequest:(NSURLRequest *)request
{
? ? //如果此請(qǐng)求是攔截到請(qǐng)求之后,接管請(qǐng)求而發(fā)起的新請(qǐng)求,則不處理。
? ? if ([request.URL.scheme isEqualToString:@"http"] &&
? ? ? ? [request valueForHTTPHeaderField:MyURLProtocolHandled] == nil)
? ? {
? ? ? ? return YES;
? ? }
? ? return NO;
}
+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request
{
? ? return request;
}
+ (BOOL)requestIsCacheEquivalent:(NSURLRequest *)a
?? ? ? ? ? ? ? ? ? ? ? toRequest:(NSURLRequest *)b
{
? ? return [super requestIsCacheEquivalent:a toRequest:b];
}
- (void)startLoading
{
? ? //如果發(fā)現(xiàn)已經(jīng)存在此請(qǐng)求的緩存數(shù)據(jù),則返回緩存數(shù)據(jù),否則發(fā)起新的請(qǐng)求從服務(wù)求加載數(shù)據(jù)
? ? MyCacheData *cacheData = [NSKeyedUnarchiver unarchiveObjectWithFile:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [self cachePathForRequest:self.request]];
? ? if(cacheData != nil)
? ? {
? ? ? ? NSData *data = cacheData.data;
? ? ? ? NSURLResponse *response = cacheData.response;
? ? ? ? NSURLRequest *redirectRequest = cacheData.request;
?? ? ? ?
? ? ? ? //使用NSURLProtocolClient做請(qǐng)求轉(zhuǎn)向,直接將請(qǐng)求和數(shù)據(jù)轉(zhuǎn)發(fā)到之前的請(qǐng)求
? ? ? ? if(redirectRequest != nil)
? ? ? ? {
? ? ? ? ? ? [[self client] URLProtocol:self wasRedirectedToRequest:redirectRequest
? ? ? ? ? ? ? ? ? ? ? redirectResponse:response];
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? [[self client] URLProtocol:self didReceiveResponse:response
? ? ? ? ? ? ? ? ? ? cacheStoragePolicy:NSURLCacheStorageNotAllowed];
? ? ? ? ? ? [[self client] URLProtocol:self didLoadData:data];
? ? ? ? ? ? [[self client] URLProtocolDidFinishLoading:self];
? ? ? ? }
? ? }
? ? else
? ? {
//接管此網(wǎng)絡(luò)請(qǐng)求,發(fā)起一個(gè)新的請(qǐng)求,后續(xù)會(huì)將新請(qǐng)求拿到的數(shù)據(jù)交給之前的舊請(qǐng)求
? ? ? ? NSMutableURLRequest *connectionRequest = [[self request] mutableCopyWorkaround];
? ? ? ? //增加標(biāo)記,標(biāo)示是由我們接管而發(fā)出的請(qǐng)求
? ? ? ? [connectionRequest setValue:@"Tag" forHTTPHeaderField:MyURLProtocolHandled];
? ? ? ? self.connection = [NSURLConnection connectionWithRequest:connectionRequest
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? delegate:self];
? ? }
}
- (void)stopLoading
{
? ? [self.connection cancel];
? ? self.connection = nil;
}
#pragma mark - 網(wǎng)絡(luò)請(qǐng)求代理
- (NSURLRequest *)connection:(NSURLConnection *)connection
?? ? ? ? ? ? willSendRequest:(NSURLRequest *)request
? ? ? ? ? ? redirectResponse:(NSURLResponse *)response
{
? ? if(response != nil)
? ? {
? ? ? ? NSMutableURLRequest *redirectableRequest = [request mutableCopyWorkaround];
?? ? ? ?
? ? ? ? //緩存數(shù)據(jù)
? ? ? ? MyCacheData *cacheData = [MyCacheData new];
? ? ? ? [cacheData setData:self.httpData];
? ? ? ? [cacheData setResponse:response];
? ? ? ? [cacheData setRequest:redirectableRequest];
?? ? ? ?
? ? ? ? [NSKeyedArchiver archiveRootObject:cacheData
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? toFile:[self cachePathForRequest:[self request]]];
?? ? ? ?
? ? ? ? //將請(qǐng)求和緩存的響應(yīng)數(shù)據(jù)轉(zhuǎn)向到之前的請(qǐng)求
? ? ? ? [[self client] URLProtocol:self wasRedirectedToRequest:redirectableRequest
? ? ? ? ? ? ? ? ? redirectResponse:response];
? ? ? ? return redirectableRequest ;
? ? }
? ? return request;
}
- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection
{
? ? return YES;
}
- (void)connection:(NSURLConnection *)connection
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
? ? [self.client URLProtocol:self didReceiveAuthenticationChallenge:challenge];
}
- (void)connection:(NSURLConnection *)connection
didCancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
? ? [self.client URLProtocol:self didCancelAuthenticationChallenge:challenge];
}
- (void)connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse *)response
{
? ? //保存響應(yīng)對(duì)象
? ? self.response = response;
? ? [self.client URLProtocol:self didReceiveResponse:response
? ? ? ? ? cacheStoragePolicy:NSURLCacheStorageNotAllowed];
}
- (void)connection:(NSURLConnection *)connection
? ? didReceiveData:(NSData *)data
{
? ? [self.client URLProtocol:self didLoadData:data];
?? ?
? ? //保存服務(wù)器返回的數(shù)據(jù)
? ? if(self.httpData == nil) {
? ? ? ? self.httpData = [NSMutableData dataWithData: data];
? ? }
? ? else
? ? {
? ? ? ? [self.httpData appendData:data];
? ? }
}
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
? ? ? ? ? ? ? ? ? willCacheResponse:(NSCachedURLResponse *)cachedResponse
{
? ? return cachedResponse;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
? ? [self.client URLProtocolDidFinishLoading:self];
?? ?
? ? //請(qǐng)求加載完畢之后,將數(shù)據(jù)緩存
? ? MyCacheData *cacheData = [MyCacheData new];
? ? [cacheData setData:self.httpData];
? ? [cacheData setResponse:self.response];
? ? [NSKeyedArchiver archiveRootObject:cacheData
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? toFile:[self cachePathForRequest:self.request]];
?? ?
? ? self.connection = nil;
? ? self.httpData = nil;
? ? self.response = nil;
}
- (void)connection:(NSURLConnection *)connection
? didFailWithError:(NSError *)error
{
? ? [self.client URLProtocol:self didFailWithError:error];
?? ?
? ? self.connection = nil;
? ? self.httpData = nil;
? ? self.response = nil;
}
#pragma mark - 為請(qǐng)求創(chuàng)建緩存路徑
- (NSString *)cachePathForRequest:(NSURLRequest *)aRequest
{
? ? NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSUserDomainMask, YES) lastObject];
? ? return [cachesPath stringByAppendingPathComponent:
? ? ? ? ? ? [NSString stringWithFormat:@"%ld", [[[aRequest URL] absoluteString] hash]]];
}
@end
@implementation NSURLRequest (MutableCopyWorkaround)
- (id) mutableCopyWorkaround {
? ? NSMutableURLRequest *mutableURLRequest = [[NSMutableURLRequest alloc]
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? initWithURL:[self URL]
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cachePolicy:[self cachePolicy]
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? timeoutInterval:[self timeoutInterval]];
? ? [mutableURLRequest setAllHTTPHeaderFields:[self allHTTPHeaderFields]];
? ? return mutableURLRequest;
}
@end
@implementation MyCacheData
-(id) initWithCoder:(NSCoder *) aDecoder
{
? ? self = [super init];
? ? if(!self) {
? ? ? ? return nil;
? ? }
? ? [self setData:[aDecoder decodeObjectForKey:@"data"]];
? ? [self setRequest:[aDecoder decodeObjectForKey:@"request"]];
? ? [self setResponse:[aDecoder decodeObjectForKey:@"response"]];
?? ?
? ? return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder
{
? ? [aCoder encodeObject:[self data] forKey:@"data"];
? ? [aCoder encodeObject:[self request] forKey:@"request"];
? ? [aCoder encodeObject:[self response] forKey:@"response"];
}
@end
部分代碼轉(zhuǎn)自:http://tanlimin201.blog.163.com/blog/static/38171407201383032914736/
總結(jié)
以上是生活随笔為你收集整理的使用NSURLProtocol实现离线缓存的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: PBOC3.0中使用的国密SM2算法
- 下一篇: spring boot controll