diff --git a/ASHWebURLProtocol.xcodeproj/project.pbxproj b/ASHWebURLProtocol.xcodeproj/project.pbxproj index de4c160..0b8f02b 100644 --- a/ASHWebURLProtocol.xcodeproj/project.pbxproj +++ b/ASHWebURLProtocol.xcodeproj/project.pbxproj @@ -13,6 +13,7 @@ A9CA81F41DDC39990013F919 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A9CA81F21DDC39990013F919 /* Main.storyboard */; }; A9CA81F61DDC39990013F919 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A9CA81F51DDC39990013F919 /* Assets.xcassets */; }; A9CA81F91DDC39990013F919 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A9CA81F71DDC39990013F919 /* LaunchScreen.storyboard */; }; + A9CA82021DDC3ADB0013F919 /* ASHWebNSURLProtocol.m in Sources */ = {isa = PBXBuildFile; fileRef = A9CA82011DDC3ADB0013F919 /* ASHWebNSURLProtocol.m */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -26,6 +27,8 @@ A9CA81F51DDC39990013F919 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; A9CA81F81DDC39990013F919 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; A9CA81FA1DDC39990013F919 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + A9CA82001DDC3ADB0013F919 /* ASHWebNSURLProtocol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASHWebNSURLProtocol.h; sourceTree = ""; }; + A9CA82011DDC3ADB0013F919 /* ASHWebNSURLProtocol.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASHWebNSURLProtocol.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -67,6 +70,8 @@ A9CA81F71DDC39990013F919 /* LaunchScreen.storyboard */, A9CA81FA1DDC39990013F919 /* Info.plist */, A9CA81E91DDC39990013F919 /* Supporting Files */, + A9CA82001DDC3ADB0013F919 /* ASHWebNSURLProtocol.h */, + A9CA82011DDC3ADB0013F919 /* ASHWebNSURLProtocol.m */, ); path = ASHWebURLProtocol; sourceTree = ""; @@ -110,6 +115,7 @@ TargetAttributes = { A9CA81E51DDC39990013F919 = { CreatedOnToolsVersion = 8.0; + DevelopmentTeam = X75CUV458P; ProvisioningStyle = Automatic; }; }; @@ -150,6 +156,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + A9CA82021DDC3ADB0013F919 /* ASHWebNSURLProtocol.m in Sources */, A9CA81F11DDC39990013F919 /* ViewController.m in Sources */, A9CA81EE1DDC39990013F919 /* AppDelegate.m in Sources */, A9CA81EB1DDC39990013F919 /* main.m in Sources */, @@ -270,7 +277,9 @@ isa = XCBuildConfiguration; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + DEVELOPMENT_TEAM = X75CUV458P; INFOPLIST_FILE = ASHWebURLProtocol/Info.plist; + IPHONEOS_DEPLOYMENT_TARGET = 8.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.ash.ASHWebURLProtocol; PRODUCT_NAME = "$(TARGET_NAME)"; @@ -281,7 +290,9 @@ isa = XCBuildConfiguration; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + DEVELOPMENT_TEAM = X75CUV458P; INFOPLIST_FILE = ASHWebURLProtocol/Info.plist; + IPHONEOS_DEPLOYMENT_TARGET = 8.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.ash.ASHWebURLProtocol; PRODUCT_NAME = "$(TARGET_NAME)"; @@ -307,6 +318,7 @@ A9CA81FF1DDC39990013F919 /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; diff --git a/ASHWebURLProtocol.xcodeproj/project.xcworkspace/xcuserdata/xmfish.xcuserdatad/UserInterfaceState.xcuserstate b/ASHWebURLProtocol.xcodeproj/project.xcworkspace/xcuserdata/xmfish.xcuserdatad/UserInterfaceState.xcuserstate index c77a977..87909f4 100644 Binary files a/ASHWebURLProtocol.xcodeproj/project.xcworkspace/xcuserdata/xmfish.xcuserdatad/UserInterfaceState.xcuserstate and b/ASHWebURLProtocol.xcodeproj/project.xcworkspace/xcuserdata/xmfish.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ASHWebURLProtocol.xcodeproj/xcuserdata/xmfish.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/ASHWebURLProtocol.xcodeproj/xcuserdata/xmfish.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist new file mode 100644 index 0000000..dd6c53f --- /dev/null +++ b/ASHWebURLProtocol.xcodeproj/xcuserdata/xmfish.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -0,0 +1,199 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ASHWebURLProtocol.xcodeproj/xcuserdata/xmfish.xcuserdatad/xcschemes/ASHWebURLProtocol.xcscheme b/ASHWebURLProtocol.xcodeproj/xcuserdata/xmfish.xcuserdatad/xcschemes/ASHWebURLProtocol.xcscheme index b393a54..b4b0b50 100644 --- a/ASHWebURLProtocol.xcodeproj/xcuserdata/xmfish.xcuserdatad/xcschemes/ASHWebURLProtocol.xcscheme +++ b/ASHWebURLProtocol.xcodeproj/xcuserdata/xmfish.xcuserdatad/xcschemes/ASHWebURLProtocol.xcscheme @@ -61,6 +61,13 @@ ReferencedContainer = "container:ASHWebURLProtocol.xcodeproj"> + + + + diff --git a/ASHWebURLProtocol/ASHWebNSURLProtocol.h b/ASHWebURLProtocol/ASHWebNSURLProtocol.h new file mode 100644 index 0000000..b304950 --- /dev/null +++ b/ASHWebURLProtocol/ASHWebNSURLProtocol.h @@ -0,0 +1,13 @@ +// +// ASHWebNSURLProtocol.h +// ASHWebURLProtocol +// +// Created by xmfish on 16/11/16. +// Copyright © 2016年 ash. All rights reserved. +// + +#import + +@interface ASHWebNSURLProtocol : NSURLProtocol + +@end diff --git a/ASHWebURLProtocol/ASHWebNSURLProtocol.m b/ASHWebURLProtocol/ASHWebNSURLProtocol.m new file mode 100644 index 0000000..b3e23b7 --- /dev/null +++ b/ASHWebURLProtocol/ASHWebNSURLProtocol.m @@ -0,0 +1,150 @@ +// +// ASHWebNSURLProtocol.m +// ASHWebURLProtocol +// +// Created by xmfish on 16/11/16. +// Copyright © 2016年 ash. All rights reserved. +// + +#import "ASHWebNSURLProtocol.h" + +@interface ASHCachedData : NSObject +@property (nonatomic, readwrite, strong) NSData *data; +@property (nonatomic, readwrite, strong) NSURLResponse *response; +@end + +static NSString *const kDataKey = @"data"; +static NSString *const kResponseKey = @"response"; +static NSString *const kHTTPHeaderField = @"ASH-WebView-Caching"; +@implementation ASHCachedData + +- (void)encodeWithCoder:(NSCoder *)aCoder +{ + [aCoder encodeObject:[self data] forKey:kDataKey]; + [aCoder encodeObject:[self response] forKey:kResponseKey]; +} + +- (id)initWithCoder:(NSCoder *)aDecoder +{ + self = [super init]; + if (self != nil) { + [self setData:[aDecoder decodeObjectForKey:kDataKey]]; + [self setResponse:[aDecoder decodeObjectForKey:kResponseKey]]; + } + + return self; +} +@end + +@interface ASHWebNSURLProtocol() +@property (nonatomic, strong) NSURLConnection* connection; +@property (nonatomic, strong) NSURLSession* session; +@property (nonatomic, readwrite, strong) NSMutableData *data; +@property (nonatomic, readwrite, strong) NSURLResponse *response; +@end + + +@implementation ASHWebNSURLProtocol + +- (NSString *)cachePathForRequest:(NSURLRequest *)aRequest +{ + NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; + NSString *fileName = [NSString stringWithFormat:@"%lu",[[[aRequest URL] absoluteString] hash]]; + + return [cachesPath stringByAppendingPathComponent:fileName]; +} + +- (void)appendData:(NSData *)newData +{ + if ([self data] == nil) { + [self setData:[newData mutableCopy]]; + } + else { + [[self data] appendData:newData]; + } +} + ++(BOOL)canInitWithRequest:(NSURLRequest *)request +{ + + ///通过UA 来判断是否UIWebView发起的请求 + NSString* UA = [request valueForHTTPHeaderField:@"User-Agent"]; + if ([UA containsString:@" AppleWebKit/"] == NO) { + return NO; + } + + if ([request valueForHTTPHeaderField:kHTTPHeaderField]) { + return NO; + } + return YES; +} + ++ (NSURLRequest*)canonicalRequestForRequest:(NSURLRequest *)request +{ + return request; +} + ++ (BOOL)requestIsCacheEquivalent:(NSURLRequest *)a toRequest:(NSURLRequest*)b +{ + return [super requestIsCacheEquivalent:a toRequest:b]; +} +- (void)startLoading +{ + + + NSString *cachesPath = [self cachePathForRequest:[self request]]; + ASHCachedData *cache = [NSKeyedUnarchiver unarchiveObjectWithFile:cachesPath]; + +// if (cache) { +// NSData *data = cache.data; +// +// [[self client] URLProtocol:self didReceiveResponse:cache.response cacheStoragePolicy:NSURLCacheStorageNotAllowed]; +// [[self client] URLProtocol:self didLoadData:data]; +// [[self client] URLProtocolDidFinishLoading:self]; +// return; +// } + + + NSMutableURLRequest *newRequest = [self.request mutableCopy]; + + [newRequest setValue:@"" forHTTPHeaderField:kHTTPHeaderField]; + [newRequest setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData]; + + self.connection = [NSURLConnection connectionWithRequest:newRequest + delegate:self]; + + [self.connection start]; +} +- (void)stopLoading +{ +// [self.connection cancel]; +} + +- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { + [self setResponse:response]; + [self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed]; +} + +- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { + [self.client URLProtocol:self didLoadData:data]; + [self appendData:data]; + +} +- (void)connectionDidFinishLoading:(NSURLConnection *)connection { + [self.client URLProtocolDidFinishLoading:self]; + + NSString *cachesPath = [self cachePathForRequest:[self request]]; + + + + ASHCachedData *cache = [ASHCachedData new]; + cache.data = [self data]; + cache.response = [self response]; + + [NSKeyedArchiver archiveRootObject:cache toFile:cachesPath]; +} +- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { + [self.client URLProtocol:self didFailWithError:error]; +} + +@end diff --git a/ASHWebURLProtocol/AppDelegate.m b/ASHWebURLProtocol/AppDelegate.m index 673ca8c..1132e0d 100644 --- a/ASHWebURLProtocol/AppDelegate.m +++ b/ASHWebURLProtocol/AppDelegate.m @@ -7,7 +7,7 @@ // #import "AppDelegate.h" - +#import "ASHWebNSURLProtocol.h" @interface AppDelegate () @end @@ -17,6 +17,8 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. + + [NSURLProtocol registerClass:[ASHWebNSURLProtocol class]]; return YES; } diff --git a/ASHWebURLProtocol/Info.plist b/ASHWebURLProtocol/Info.plist index 38e98af..9a21b4f 100644 --- a/ASHWebURLProtocol/Info.plist +++ b/ASHWebURLProtocol/Info.plist @@ -34,5 +34,10 @@ UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight + NSAppTransportSecurity + + NSAllowsArbitraryLoads + + diff --git a/ASHWebURLProtocol/ViewController.m b/ASHWebURLProtocol/ViewController.m index 2557e29..3a525db 100644 --- a/ASHWebURLProtocol/ViewController.m +++ b/ASHWebURLProtocol/ViewController.m @@ -8,18 +8,31 @@ #import "ViewController.h" -@interface ViewController () +@interface ViewController () +@property(nonatomic,strong) UIWebView* webview;; @end @implementation ViewController + + - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. + _webview = [[UIWebView alloc] init]; + _webview.frame = self.view.bounds; + [self.view addSubview:_webview]; + + [self loadWebView]; } - +- (void)loadWebView +{ + NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"]; + NSURLRequest *request = [NSURLRequest requestWithURL:url]; + [self.webview loadRequest:request]; +} - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.