|
| 1 | +// |
| 2 | +// NSURLWebViewProtocol.m |
| 3 | +// |
| 4 | +// Created by Aaron Wardle on 29/11/2013. |
| 5 | +// http://www.aaronwardle.co.uk |
| 6 | +// |
| 7 | + |
| 8 | +#import "NSURLWebViewProtocol.h" |
| 9 | + |
| 10 | +@implementation NSURLWebViewProtocol |
| 11 | + |
| 12 | + |
| 13 | ++ (BOOL)canInitWithRequest:(NSURLRequest *)request { |
| 14 | + if ([request.URL.scheme caseInsensitiveCompare:kDocumentsImageUrl] == NSOrderedSame) { |
| 15 | + return YES; |
| 16 | + } |
| 17 | + |
| 18 | + if ([request.URL.scheme caseInsensitiveCompare:kBundleImageUrl] == NSOrderedSame) { |
| 19 | + return YES; |
| 20 | + } |
| 21 | + |
| 22 | + return NO; |
| 23 | +} |
| 24 | + |
| 25 | + |
| 26 | ++ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request { |
| 27 | + return request; |
| 28 | +} |
| 29 | + |
| 30 | +- (void)startLoading { |
| 31 | + |
| 32 | + NSString *fileExtension; |
| 33 | + NSString *fileName; |
| 34 | + NSString *urlPrefix; |
| 35 | + NSString *filePath; |
| 36 | + |
| 37 | + [self extractFileName:&fileName fileExtension:&fileExtension urlPrefix:&urlPrefix]; |
| 38 | + |
| 39 | + |
| 40 | + NSURLResponse *response =[[NSURLResponse alloc]initWithURL:self.request.URL |
| 41 | + MIMEType:nil expectedContentLength:-1 |
| 42 | + textEncodingName:nil]; |
| 43 | + |
| 44 | + |
| 45 | + if ([urlPrefix isEqualToString:kBundleImageUrl]) { |
| 46 | + filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:fileExtension]; |
| 47 | + } else { |
| 48 | + filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; |
| 49 | + filePath = [filePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", fileName, fileExtension]]; |
| 50 | + } |
| 51 | + |
| 52 | + NSData *data = [NSData dataWithContentsOfFile:filePath]; |
| 53 | + |
| 54 | + [[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed]; |
| 55 | + [[self client] URLProtocol:self didLoadData:data]; |
| 56 | + [[self client] URLProtocolDidFinishLoading:self]; |
| 57 | + |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | +- (void)extractFileName:(NSString**)fileName fileExtension:(NSString **)extension urlPrefix:(NSString **)urlPrefix { |
| 62 | + |
| 63 | + NSString *urlString = self.request.URL.absoluteString; |
| 64 | + *extension = [urlString pathExtension]; |
| 65 | + |
| 66 | + |
| 67 | + NSRange isRange = [urlString rangeOfString:kBundleImageUrl options:NSCaseInsensitiveSearch]; |
| 68 | + if (isRange.location == 0) { |
| 69 | + *urlPrefix = kBundleImageUrl; |
| 70 | + } else { |
| 71 | + isRange = [urlString rangeOfString:kDocumentsImageUrl options:NSCaseInsensitiveSearch]; |
| 72 | + if (isRange.location == 0) { |
| 73 | + *urlPrefix = kDocumentsImageUrl; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + urlString = [urlString stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@://", *urlPrefix] withString:@""]; |
| 78 | + |
| 79 | + *fileName = [urlString stringByDeletingPathExtension]; |
| 80 | + |
| 81 | +} |
| 82 | + |
| 83 | + |
| 84 | +- (void)stopLoading { |
| 85 | + |
| 86 | +} |
| 87 | + |
| 88 | +@end |
0 commit comments