缓存webview图片资源的两种方式

在使用iOS的webview的时候发现这样一个问题,加载一个网页进来,webview会负责缓存页面里的css,js和图片这些资源。但是这个缓存不受开发者控制,缓存时间非常短.所以为了节省用户流量,大量的Hybird混合应用和电商类应用都在研究H5页面热更新和图片交由本地保存的策略,今天我们来研究一下如何缓存webivew的图片资源。

第一种方式:NSURLCache

作为iOS御用的缓存类,NSURLCache给我们提供了一个简单的缓存实现方式,但在使用的时候,某些情况下,应用中的系统组件会将缓存的内存容量设为0MB,这就禁用了缓存。解决这个行为的一种方式就是通过自己的实现子类化NSURLCache,拒绝将内存缓存大小设为0。
在我们仅仅为了实现一个缓存图片的类的时候,我们的代码极其简单,就是继承NSURLCache,重载下面这两个方法,就实现类图片缓存和读取:

- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request {
                                                                                                                                                                                                                          
    NSString *pathString = [[request URL] absoluteString];
                                                                                                                                                                                                                          
    if(![pathString hasSuffix:@".jpg"] || ![pathString hasSuffix:@".png"]) {
        return[super cachedResponseForRequest:request];
    }
                                                                                                                                                                                                                          
    if([[BGURLCache sharedCache] hasDataForURL:pathString]) {
        NSData *data = [[BGURLCache sharedCache] dataForURL:pathString];
        NSURLResponse *response = [[NSURLResponse alloc] initWithURL:[request URL]
                                                             MIMEType:[pathString hasSuffix:@".jpg"]?@"image/jpg":@"image/png"
                                                expectedContentLength:[data length]
                                                     textEncodingName:nil];
        return  [[NSCachedURLResponse alloc] initWithResponse:response data:data];        
    }
    return[super cachedResponseForRequest:request];
}
                                                                                                                                                                                                                      
- (void)storeCachedResponse:(NSCachedURLResponse *)cachedResponse forRequest:(NSURLRequest *)request {
    NSString *pathString = [[request URL] absoluteString];
    if(![pathString hasSuffix:@".jpg"] || ![pathString hasSuffix:@".png"]) {
        [super storeCachedResponse:cachedResponse forRequest:request];
        return;
    }
                                                                                                                                                                                                                          
    [[BGURLCache sharedCache] storeData:cachedResponse.data forURL:pathString];
}

NSURLCache的坑

使用NSURLCache做缓存看起来简单又好用,但是为什么各种大佬都不建议用呢?具体到我这个需求,是因为下面这几个坑:
1.只能用在get请求里面,post可以洗洗睡了。
2.需要服务器定义数据是否发生变化,需要在请求头里查找是否修改了的信息。公司服务器没有定义的话,就不能够判断读取的缓存数据是否需要刷新。
3.删除缓存的removeCachedResponseForRequest 这个方法是无效的.所以缓存是不会被删除的—只有删除全部缓存才有效。

总结

不能删除对应的缓存方案是没有意义的,所以我放弃了这个方案。

第二种方案:NSURLProtocol

NSURLProtocol或许是URL加载系统中最功能强大但同时也是最晦涩的部分了。它是一个抽象类,你可以通过子类化来定义新的或已经存在的URL加载行为。还好我们的需求只是做一个图片的缓存需求,不然就抓瞎了,在具体到这个类的时候我们要做的也很简单,拦截图片加载请求,转为从本地文件加载。
1.我们要认识NSURLProtocol,首先它是一个抽象类,不能够直接使用必须被子类化之后才能使用。子类化 NSURLProtocol 的第一个任务就是告诉它要控制什么类型的网络请求。比如说如果你想要当本地有资源的时候请求直接使用本地资源文件,那么相关的请求应该对应已有资源的文件名。
这部分逻辑定义在

+ (BOOL)canInitWithRequest:(NSURLRequest *)request;

中,如果返回 YES,该请求就会被其控制。返回 NO 则直接跳入下一个Protocol,一句话,我们可以在里面完成拦截图片加载请求,转为从本地文件加载的大概逻辑。
2.获取和设置一个请求对象的当前状态,可以在Protocol的各种方法中传递当前request我们自定义的状态。核心方法是:

+ (nullable id)propertyForKey:(NSString *)key inRequest:(NSURLRequest *)request;
+ (void)setProperty:(id)value forKey:(NSString *)key inRequest:(NSMutableURLRequest *)request;
+ (void)removePropertyForKey:(NSString *)key inRequest:(NSMutableURLRequest *)request;

3.最最重要的方法

 -startLoading 
 -stopLoading

不同的自定义子类在调用这两个方法是会传入不同的内容,但共同点都是要围绕protocol的client属性进行操作,在对-startLoading 和-stopLoading的实现中,需要在恰当的时候让client调用每一个delegate方法。我们在startloading中初始化NSURLSessionDataTask,在session的代理方法中传递数据给client的代理方法。在stoploading中结束当前datatask。
4.向系统注册该NSURLProtocol,当请求被加载时,系统会向每一个注册过的protocol询问是否能控制该请求,第一个通过+canInitWithRequest: 回答为 YES 的protocol就会控制该请求。URLProtocol会被以注册顺序的反序访问,所以当在 -application:didFinishLoadingWithOptions:方法中调用 [NSURLProtocol registerClass:[BGURLProtocol class]]; 时,你自己写的protocol比其他内建的protocol拥有更高的优先级。
4.核心代码
BGURLProtocol.m:

+ (BOOL)canInitWithRequest:(NSURLRequest *)request
{
    NSString *urlString = request.URL.absoluteString;
    NSString* extension = request.URL.pathExtension;
    if([NSURLProtocol propertyForKey:@"ProtocolHandledKey" inRequest:request]) {
        return NO;
    }
    BOOL isImage = [@[@"png", @"jpeg", @"gif", @"jpg"] indexOfObjectPassingTest:^BOOL(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        return [extension compare:obj options:NSCaseInsensitiveSearch] == NSOrderedSame;
    }] != NSNotFound;
    if (isImage)
    {
        NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
        filePath = [filePath stringByAppendingPathComponent:[[urlString componentsSeparatedByString:@"/"] lastObject]];
        if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
        {
            return YES;
        }
        else
        {
            static NSInteger requestCount = 0;
            static NSInteger requestRefresh = 0;
            NSMutableURLRequest *newRequest = [request mutableCopy];
            [NSURLProtocol setProperty:@YES forKey:@"ProtocolHandledKey" inRequest:newRequest];
            NSString *url = [@"http://www.baidu.com/img/" stringByAppendingString:[[urlString componentsSeparatedByString:@"/"] lastObject]];
            requestCount++;
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                requestRefresh = 1;
                if (requestCount == 0)
                {
                    [[NSNotificationCenter defaultCenter] postNotificationName:kImageDownloadNotification object:[[urlString componentsSeparatedByString:@"/"] lastObject]];
                }
            });
            [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:url] options:SDWebImageDownloaderUseNSURLCache progress:^(NSInteger receivedSize, NSInteger expectedSize) {
                
            } completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
                requestCount--;
                NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
                filePath = [filePath stringByAppendingPathComponent:[[urlString componentsSeparatedByString:@"/"] lastObject]];
                [data writeToFile:filePath atomically:YES];
                if (requestCount == 0 && requestRefresh == 1)
                {
                    [[NSNotificationCenter defaultCenter] postNotificationName:kImageDownloadNotification object:[[urlString componentsSeparatedByString:@"/"] lastObject]];
                }
                
            }];
        }
        
        return YES;
    }else{
        return YES;
    }
}
-(void)startLoading
{
    NSMutableURLRequest *newRequest = [self.request mutableCopy];
    [NSURLProtocol setProperty:@YES forKey:@"ProtocolHandledKey" inRequest:newRequest];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]
                                                          delegate:self
                                                     delegateQueue:[[NSOperationQueue alloc] init]];

    self.connection = [session dataTaskWithRequest:newRequest];
     [self.connection resume];
}

- (void)stopLoading {
    [self.connection cancel];
    self.connection =nil;
}

-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data{
    [self.client URLProtocol:self didLoadData:data];
}

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
didReceiveResponse:(NSURLResponse *)response
 completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler{
    if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
        NSURLResponse *retResponse = [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:response.URL.absoluteString] statusCode:httpResponse.statusCode HTTPVersion:(__bridge NSString *)kCFHTTPVersion1_1 headerFields:httpResponse.allHeaderFields];
        [self.client URLProtocol:self didReceiveResponse:retResponse cacheStoragePolicy:NSURLCacheStorageNotAllowed];
    } else {
        NSURLResponse *retResponse = [[NSURLResponse alloc] initWithURL:[NSURL URLWithString:response.URL.absoluteString] MIMEType:response.MIMEType expectedContentLength:response.expectedContentLength textEncodingName:response.textEncodingName];
        [self.client URLProtocol:self didReceiveResponse:retResponse cacheStoragePolicy:NSURLCacheStorageNotAllowed];
    }
    completionHandler(NSURLSessionResponseAllow);
    
}

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
    // 请求完成,成功或者失败的处理
    if (error) {
        [self.client URLProtocol:self didFailWithError:error];
    }else{
        [self.client URLProtocolDidFinishLoading:self];
    }
}

webview初始化:

    NSString *htmlString = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.baidu.com"] encoding:NSUTF8StringEncoding error:nil];
    
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"//www.baidu.com/img/" withString:@""];
    _htmlString = htmlString;
    NSString *baseUrl = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    [webView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:baseUrl]];

5.我们现在实现了用NSURLProtocol拦截.png和.jpg的网络请求,让UIWebView本身的图片下载发不出去,拦截的链接通过SDWebImage下载资源到本地目录,用WebView的loadHTMLString:baseURL:方法来实现读取本地目录的图片显示,当下载图片超过2秒,并且请求数为0时发送通知给webView刷新显示本地资源。但是,现在已经iOS11了啊,UIWebView内存占用太大已经跟不上时代了,WKWebiview默认不支持NSURLProtocol,所以我们得找个办法让WK支持我们子类话的protocol。所以我找到了这段:

//Class cls = NSClassFromString(@"WKBrowsingContextController");
Class cls = [[[WKWebView new] valueForKey:@"browsingContextController"] class];
SEL sel = NSSelectorFromString(@"registerSchemeForCustomProtocol:");
if ([(id)cls respondsToSelector:sel]) {    
// 把 http 和 https 请求交给 NSURLProtocol 处理  
        [(id)cls performSelector:sel withObject:@"http"];   
        [(id)cls performSelector:sel withObject:@"https"];
}

这样,我们就完成了webview缓存图片资源的需求。
参考资料:
http://bbs.csdn.net/topics/390831054
http://blog.csdn.net/jason_chen13/article/details/51984823
https://github.com/Yeatse/NSURLProtocol-WebKitSupport
http://blog.csdn.net/xanxus46/article/details/51946432
http://blog.csdn.net/u011661836/article/details/70241061

你可能感兴趣的:(缓存webview图片资源的两种方式)