|
@@ -1,81 +1,139 @@
|
|
|
//
|
|
|
// NSURLProtocolCustom.m
|
|
|
-// JianYuIOS
|
|
|
//
|
|
|
-// Created by apple on 2020/6/9.
|
|
|
-// Copyright © 2020 lixianglan. All rights reserved.
|
|
|
+// 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;
|
|
|
|
|
|
-static NSString* const FilteredKey = @"FilteredKey";
|
|
|
+@end
|
|
|
|
|
|
-@implementation NSURLProtocolCustom
|
|
|
|
|
|
-+ (BOOL)canInitWithRequest:(NSURLRequest *)request
|
|
|
-{
|
|
|
- NSString *extension = request.URL.pathExtension;
|
|
|
-
|
|
|
- BOOL isSource = [@[@"png", @"jpeg", @"gif", @"jpg", @"js", @"css"] indexOfObjectPassingTest:^BOOL(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
|
|
|
- return [extension compare:obj options:NSCaseInsensitiveSearch] == NSOrderedSame;
|
|
|
- }] != NSNotFound;
|
|
|
+@implementation NSURLProtocolCustom
|
|
|
|
|
|
- NSLog(@"检测监测监测:%@---%@",extension,[NSURLProtocol propertyForKey:FilteredKey inRequest:request]);
|
|
|
- return [NSURLProtocol propertyForKey:FilteredKey inRequest:request] == nil && isSource;
|
|
|
-}
|
|
|
-
|
|
|
-+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request
|
|
|
-{
|
|
|
- return request;
|
|
|
++ (BOOL)canInitWithRequest:(NSURLRequest *)request {
|
|
|
+ if ([NSURLProtocol propertyForKey:protocolKey inRequest:request]) {
|
|
|
+ return NO;
|
|
|
+ }
|
|
|
+ NSString * url = request.URL.absoluteString;
|
|
|
+ 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 *fileName = [super.request.URL.absoluteString componentsSeparatedByString:@"/"].lastObject;
|
|
|
- NSLog(@"fileName is %@",fileName);
|
|
|
- //这里是获取本地资源路径 如:png,js等
|
|
|
- NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:nil];
|
|
|
- if (!path) {
|
|
|
- [self sendResponseWithData:[NSData data] mimeType:nil];
|
|
|
- return;
|
|
|
+ //判断是否包含版本--
|
|
|
+ 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 = [NSString stringWithFormat:@"%@/%@",[[GlobalData shareInstance]getDocumentPath],verName];
|
|
|
+ //如果无则创建否则
|
|
|
+ if (![[GlobalData shareInstance] creatDir:pathName]) {
|
|
|
+ NSLog(@"文件夹已存在");
|
|
|
+ }else {
|
|
|
+ NSLog(@"文件夹不存在,创建");
|
|
|
+ }
|
|
|
+ NSString *filePath = [NSString stringWithFormat:@"%@/%@",pathName,[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:curData.length textEncodingName:nil];
|
|
|
+ [self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
|
|
|
+ [self.client URLProtocol:self didLoadData:curData];
|
|
|
+ [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];
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- //根据路径获取MIMEType
|
|
|
- CFStringRef pathExtension = (__bridge_retained CFStringRef)[path pathExtension];
|
|
|
- CFStringRef type = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension, NULL);
|
|
|
- CFRelease(pathExtension);
|
|
|
|
|
|
- //The UTI can be converted to a mime type:
|
|
|
- NSString *mimeType = (__bridge_transfer NSString *)UTTypeCopyPreferredTagWithClass(type, kUTTagClassMIMEType);
|
|
|
- if (type != NULL)
|
|
|
- CFRelease(type);
|
|
|
|
|
|
- //加载本地资源
|
|
|
- NSData *data = [NSData dataWithContentsOfFile:path];
|
|
|
- [self sendResponseWithData:data mimeType:mimeType];
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
-
|
|
|
-- (void)stopLoading
|
|
|
-{
|
|
|
- NSLog(@"stopLoading, something went wrong!");
|
|
|
+- (void)stopLoading {
|
|
|
+ if (self.task != nil) {
|
|
|
+ [self.task cancel];
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
-- (void)sendResponseWithData:(NSData *)data mimeType:(nullable NSString *)mimeType
|
|
|
-{
|
|
|
- // 这里需要用到MIMEType
|
|
|
- NSURLResponse *response = [[NSURLResponse alloc] initWithURL:super.request.URL
|
|
|
- MIMEType:mimeType
|
|
|
- expectedContentLength:-1
|
|
|
- textEncodingName:nil];
|
|
|
+
|
|
|
+- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler {
|
|
|
+ [[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageAllowed];
|
|
|
|
|
|
- //硬编码 开始嵌入本地资源到web中
|
|
|
- [[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
|
|
|
- [[self client] URLProtocol:self didLoadData:data];
|
|
|
- [[self client] URLProtocolDidFinishLoading:self];
|
|
|
+ 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
|