NSURLProtocolCustom.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // NSURLProtocolCustom.m
  3. //
  4. // Created by shuoyu liu on 2017/1/16.
  5. // Copyright © 2017年 shuoyu liu. All rights reserved.
  6. //
  7. #import "NSURLProtocolCustom.h"
  8. #import <UIKit/UIKit.h>
  9. #import "LXMD5.h"
  10. static NSString* const protocolKey = @"protocolKey";
  11. @interface NSURLProtocolCustom ()<NSURLSessionDelegate>
  12. @property (nonnull,strong) NSURLSessionDataTask *task;
  13. @end
  14. @implementation NSURLProtocolCustom
  15. + (BOOL)canInitWithRequest:(NSURLRequest *)request {
  16. if ([NSURLProtocol propertyForKey:protocolKey inRequest:request]) {
  17. return NO;
  18. }
  19. NSString * url = request.URL.absoluteString;
  20. //&&![url containsString:@"common.js"]
  21. if ([url hasPrefix:@"res://"]) {
  22. NSLog(@"协议拦截成功:%@",url);
  23. return YES;
  24. }
  25. return NO;
  26. }
  27. + (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request {
  28. NSMutableURLRequest *mutableReqeust = [request mutableCopy];
  29. if ([mutableReqeust.URL host].length == 0) {
  30. return mutableReqeust;
  31. }
  32. NSString *curStr = [mutableReqeust.URL absoluteString];
  33. // 替换域名
  34. NSString *urlString = [curStr stringByReplacingOccurrencesOfString:@"res://" withString:@"http://"];
  35. NSURL *urlll = [NSURL URLWithString:urlString];
  36. mutableReqeust.URL = urlll;
  37. return mutableReqeust;
  38. }
  39. - (void)startLoading
  40. {
  41. //判断是否包含版本--
  42. NSString *curUrl = [NSString stringWithFormat:@"%@",self.request.URL.absoluteString];
  43. if ([curUrl containsString:@"?v="]) {
  44. NSString *verName = @"";
  45. NSLog(@"有相关版本号url:%@",curUrl);
  46. verName =[curUrl componentsSeparatedByString:@"?v="].lastObject;
  47. NSLog(@"版本目录名:%@",verName);
  48. //查询缓存目录
  49. NSString *pathName = [[[GlobalData shareInstance]getDocumentPath] stringByAppendingPathComponent:verName];
  50. //如果无则创建否则
  51. if (![[GlobalData shareInstance] creatDir:pathName]) {
  52. NSLog(@"文件夹已存在");
  53. }else {
  54. NSLog(@"文件夹不存在,创建");
  55. }
  56. NSString *filePath = [pathName stringByAppendingPathComponent:[LXMD5 md532BitLower:curUrl]];
  57. NSData *fileData = [[GlobalData shareInstance] readFile:filePath];
  58. if (fileData!=nil) {//存在查询数据
  59. NSLog(@"沙盒有数据");
  60. NSURLResponse* response = [[NSURLResponse alloc] initWithURL:self.request.URL MIMEType:nil expectedContentLength:fileData.length textEncodingName:nil];
  61. [self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
  62. [self.client URLProtocol:self didLoadData:fileData];
  63. [self.client URLProtocolDidFinishLoading:self];
  64. }else {
  65. //沙盒添加数据
  66. NSLog(@"沙盒无数据");
  67. NSData *data = [NSData dataWithContentsOfURL:self.request.URL];
  68. [[GlobalData shareInstance] creatFile:filePath withData:data];
  69. // NSData *curData = [[GlobalData shareInstance] readFile:filePath];
  70. NSURLResponse* response = [[NSURLResponse alloc] initWithURL:self.request.URL MIMEType:nil expectedContentLength:data.length textEncodingName:nil];
  71. [self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
  72. [self.client URLProtocol:self didLoadData:data];
  73. [self.client URLProtocolDidFinishLoading:self];
  74. }
  75. }else {
  76. //查询项目目录
  77. NSString *fileName = [super.request.URL.absoluteString componentsSeparatedByString:@"/"].lastObject;
  78. NSLog(@"无V当前fileName:%@",fileName);
  79. NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:nil];
  80. if (path) {
  81. NSLog(@"项目目录有数据");
  82. NSData *data = [NSData dataWithContentsOfFile:path];
  83. NSURLResponse* response = [[NSURLResponse alloc] initWithURL:self.request.URL MIMEType:nil expectedContentLength:data.length textEncodingName:nil];
  84. [self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageAllowed];
  85. [self.client URLProtocol:self didLoadData:data];
  86. [self.client URLProtocolDidFinishLoading:self];
  87. }else {
  88. NSLog(@"项目目录本地无数据");
  89. NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:nil];
  90. self.task = [session dataTaskWithRequest:self.request];
  91. [self.task resume];
  92. }
  93. }
  94. }
  95. - (void)stopLoading {
  96. if (self.task != nil) {
  97. [self.task cancel];
  98. }
  99. }
  100. - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler {
  101. [[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageAllowed];
  102. completionHandler(NSURLSessionResponseAllow);
  103. }
  104. - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
  105. [[self client] URLProtocol:self didLoadData:data];
  106. }
  107. - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(nullable NSError *)error {
  108. [self.client URLProtocolDidFinishLoading:self];
  109. }
  110. @end