123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- //
- // NSURLProtocolCustom.m
- //
- // Created by shuoyu liu on 2017/1/16.
- // Copyright © 2017年 shuoyu liu. All rights reserved.
- //
- #import "NSURLProtocolCustom.h"
- #import <UIKit/UIKit.h>
- #import "LXMD5.h"
- static NSString* const protocolKey = @"protocolKey";
- @interface NSURLProtocolCustom ()<NSURLSessionDelegate>
- @property (nonnull,strong) NSURLSessionDataTask *task;
- @end
- @implementation NSURLProtocolCustom
- + (BOOL)canInitWithRequest:(NSURLRequest *)request {
- if ([NSURLProtocol propertyForKey:protocolKey inRequest:request]) {
- return NO;
- }
- NSString * url = request.URL.absoluteString;
- //&&![url containsString:@"common.js"]
- if ([url hasPrefix:@"res://"]) {
- NSLog(@"协议拦截成功:%@",url);
- return YES;
- }
- return NO;
- }
- + (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request {
- NSMutableURLRequest *mutableReqeust = [request mutableCopy];
- if ([mutableReqeust.URL host].length == 0) {
- return mutableReqeust;
- }
- NSString *curStr = [mutableReqeust.URL absoluteString];
- // 替换域名
- NSString *urlString = [curStr stringByReplacingOccurrencesOfString:@"res://" withString:@"http://"];
- NSURL *urlll = [NSURL URLWithString:urlString];
- mutableReqeust.URL = urlll;
- return mutableReqeust;
- }
-
- - (void)startLoading
- {
- //判断是否包含版本--
- NSString *curUrl = [NSString stringWithFormat:@"%@",self.request.URL.absoluteString];
- if ([curUrl containsString:@"?v="]) {
- NSString *verName = @"";
- NSLog(@"有相关版本号url:%@",curUrl);
- verName =[curUrl componentsSeparatedByString:@"?v="].lastObject;
- NSLog(@"版本目录名:%@",verName);
- //查询缓存目录
- NSString *pathName = [[[GlobalData shareInstance]getDocumentPath] stringByAppendingPathComponent:verName];
- //如果无则创建否则
- if (![[GlobalData shareInstance] creatDir:pathName]) {
- NSLog(@"文件夹已存在");
- }else {
- NSLog(@"文件夹不存在,创建");
- }
- NSString *filePath = [pathName stringByAppendingPathComponent:[LXMD5 md532BitLower:curUrl]];
- NSData *fileData = [[GlobalData shareInstance] readFile:filePath];
- if (fileData!=nil) {//存在查询数据
- NSLog(@"沙盒有数据");
- NSURLResponse* response = [[NSURLResponse alloc] initWithURL:self.request.URL MIMEType:nil expectedContentLength:fileData.length textEncodingName:nil];
- [self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
- [self.client URLProtocol:self didLoadData:fileData];
- [self.client URLProtocolDidFinishLoading:self];
- }else {
- //沙盒添加数据
- NSLog(@"沙盒无数据");
- NSData *data = [NSData dataWithContentsOfURL:self.request.URL];
- [[GlobalData shareInstance] creatFile:filePath withData:data];
-
-
- // NSData *curData = [[GlobalData shareInstance] readFile:filePath];
-
-
- NSURLResponse* response = [[NSURLResponse alloc] initWithURL:self.request.URL MIMEType:nil expectedContentLength:data.length textEncodingName:nil];
- [self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
- [self.client URLProtocol:self didLoadData:data];
- [self.client URLProtocolDidFinishLoading:self];
- }
-
-
-
-
- }else {
- //查询项目目录
- NSString *fileName = [super.request.URL.absoluteString componentsSeparatedByString:@"/"].lastObject;
- NSLog(@"无V当前fileName:%@",fileName);
- NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:nil];
- if (path) {
- NSLog(@"项目目录有数据");
- NSData *data = [NSData dataWithContentsOfFile:path];
- NSURLResponse* response = [[NSURLResponse alloc] initWithURL:self.request.URL MIMEType:nil expectedContentLength:data.length textEncodingName:nil];
- [self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageAllowed];
- [self.client URLProtocol:self didLoadData:data];
- [self.client URLProtocolDidFinishLoading:self];
- }else {
- NSLog(@"项目目录本地无数据");
- NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:nil];
- self.task = [session dataTaskWithRequest:self.request];
- [self.task resume];
- }
- }
-
-
-
-
-
-
-
- }
- - (void)stopLoading {
- if (self.task != nil) {
- [self.task cancel];
- }
- }
- - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler {
- [[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageAllowed];
-
- completionHandler(NSURLSessionResponseAllow);
- }
- - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
- [[self client] URLProtocol:self didLoadData:data];
- }
- - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(nullable NSError *)error {
- [self.client URLProtocolDidFinishLoading:self];
- }
- @end
|