写好了

This commit is contained in:
wushenghua
2016-11-17 16:44:35 +08:00
parent f56bfc2c78
commit fb575c0b62
9 changed files with 404 additions and 3 deletions
@@ -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 = "<group>"; };
A9CA81F81DDC39990013F919 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
A9CA81FA1DDC39990013F919 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
A9CA82001DDC3ADB0013F919 /* ASHWebNSURLProtocol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASHWebNSURLProtocol.h; sourceTree = "<group>"; };
A9CA82011DDC3ADB0013F919 /* ASHWebNSURLProtocol.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASHWebNSURLProtocol.m; sourceTree = "<group>"; };
/* 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 = "<group>";
@@ -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 */
};
@@ -0,0 +1,199 @@
<?xml version="1.0" encoding="UTF-8"?>
<Bucket
type = "1"
version = "2.0">
<Breakpoints>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "ASHWebURLProtocol/ASHWebNSURLProtocol.m"
timestampString = "501060666.918845"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "54"
endingLineNumber = "54"
landmarkName = "-cachePathForRequest:"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "ASHWebURLProtocol/ASHWebNSURLProtocol.m"
timestampString = "500985187.477216"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "98"
endingLineNumber = "98"
landmarkName = "-startLoading"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "ASHWebURLProtocol/ViewController.m"
timestampString = "500982623.643233"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "34"
endingLineNumber = "34"
landmarkName = "-loadWebView"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "ASHWebURLProtocol/ASHWebNSURLProtocol.m"
timestampString = "501061252.530373"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "108"
endingLineNumber = "108"
landmarkName = "-startLoading"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "ASHWebURLProtocol/ASHWebNSURLProtocol.m"
timestampString = "501061255.922029"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "71"
endingLineNumber = "71"
landmarkName = "+canInitWithRequest:"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "ASHWebURLProtocol/ASHWebNSURLProtocol.m"
timestampString = "501062432.910644"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "125"
endingLineNumber = "125"
landmarkName = "-connection:didReceiveResponse:"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "ASHWebURLProtocol/ASHWebNSURLProtocol.m"
timestampString = "501062434.247891"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "129"
endingLineNumber = "129"
landmarkName = "-connection:didReceiveData:"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "ASHWebURLProtocol/ASHWebNSURLProtocol.m"
timestampString = "501062436.390585"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "136"
endingLineNumber = "136"
landmarkName = "-connectionDidFinishLoading:"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "ASHWebURLProtocol/ASHWebNSURLProtocol.m"
timestampString = "501062440.37441"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "147"
endingLineNumber = "147"
landmarkName = "-connection:didFailWithError:"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "ASHWebURLProtocol/ASHWebNSURLProtocol.m"
timestampString = "501062449.989783"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "77"
endingLineNumber = "77"
landmarkName = "+canInitWithRequest:"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "ASHWebURLProtocol/ASHWebNSURLProtocol.m"
timestampString = "501062625.657318"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "99"
endingLineNumber = "99"
landmarkName = "-startLoading"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "ASHWebURLProtocol/ASHWebNSURLProtocol.m"
timestampString = "501062634.280876"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "135"
endingLineNumber = "135"
landmarkName = "-connectionDidFinishLoading:"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
@@ -61,6 +61,13 @@
ReferencedContainer = "container:ASHWebURLProtocol.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<EnvironmentVariables>
<EnvironmentVariable
key = "OS_ACTIVITY_MODE"
value = "disable"
isEnabled = "YES">
</EnvironmentVariable>
</EnvironmentVariables>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
+13
View File
@@ -0,0 +1,13 @@
//
// ASHWebNSURLProtocol.h
// ASHWebURLProtocol
//
// Created by xmfish on 16/11/16.
// Copyright © 2016年 ash. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface ASHWebNSURLProtocol : NSURLProtocol<NSURLConnectionDelegate>
@end
+150
View File
@@ -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<NSCoding>
@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
+3 -1
View File
@@ -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;
}
+5
View File
@@ -34,5 +34,10 @@
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
</dict>
</plist>
+15 -2
View File
@@ -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.