1.APPdelegate里面注册NSURLProtocol
[NSURLProtocolregisterClass:[CustomURLProtocolclass]];
CustomURLProtorcol 是继承NSURLProtocol的类
2.遵守代理创建NSURLProtocol对象
@interfaceCustomURLProtocol()
@property(nonatomic,strong)NSURLConnection*connection
staticNSString*constURLProtocolHandledKey =@"URLProtocolHandledKey";//声明键值
3.判断拦截请求是何种请求
4.判断拦截的内容是否和上传一样,如果一样就缓存上一次的
5.开始读取本地加载的数据
- (void)startLoading
{
NSURLRequest*request =self.request;
NSString*originUrlString = [request.URLabsoluteString];
NSLog(@"完整域名%@",originUrlString);
NSString*originHostString = [request.URLhost];
NSLog(@"主域名%@",originHostString);
if([Gamedictiondiccreategamedic].gamedictiondic==nil){
[Gamedictiondiccreategamedic].gamedictiondic=[NSMutableDictionarynew];
}
NSMutableDictionary*loadImageDic = [[NSMutableDictionaryalloc]init];
NSArray* array=[[NSArrayalloc]init];
array=@[@"bgFire.png",
@"avatar.jpg",
@"battle1.png",
@"battle2.png",
@"battle3.png",
@"battle4.png",
@"battle5.png",
@"battle6.png",
@"bg.jpg",
@"bgFire.png",
@"bgLoading.jpg",
@"blood.png",
@"booldIndicator.png",
@"bullet.png",
@"button1.png",
@"button2.png",
@"button3.png",
@"chat.png",
@"failure_bg.jpg",
@"failure_bg.png",
@"logout.png",
@"male.gif",
@"peng.png",
@"setting.png",
@"setting_bg.jpg",
@"share.png",
@"soundMute.png",
@"soundPlaying.png",
@"starting.jpg",
@"wall.png",
@"women.gif",
@"won_bg.jpg",
@"won_bg.png"
];
for(inti =0;i < (int)array.count;i++){
[loadImageDicsetObject:@"1"forKey:array[i]];
}
NSString* exestr=[originUrlStringpathExtension];
NSLog(@"后缀名%@",exestr);
BooleanimageFlag =false;
if([exestrisEqualToString:@"png"] || [exestrisEqualToString:@"jpg"]){
imageFlag =true;
}
if(imageFlag && [originHostStringisEqualToString:@"testpao.miusky.com"]){
NSString* lastimagestr=[originUrlStringsubstringFromIndex:53];
NSString* firstimagstr=[originUrlStringsubstringToIndex:53];
NSLog(@"截取前端的图片%@",firstimagstr);
NSLog(@"截取后端的图片的url%@",lastimagestr);
if([lastimagestrisEqualToString:@"bgFire.png"]){
NSLog(@"%@",loadImageDic);
}
NSLog(@"截取后端的图片的after%@",[loadImageDicobjectForKey:[NSStringstringWithFormat:@"%@",lastimagestr]]);
NSLog(@"afar is %@",loadImageDic);
if([firstimagstrisEqualToString:[NSStringstringWithFormat:@"http://testpao.miusky.com:18080/statics/front/images/"]] && [loadImageDicobjectForKey:[NSStringstringWithFormat:@"%@",lastimagestr]] !=nil) {
NSString* bundlePath = [[NSBundlemainBundle]pathForResource:@"photo"ofType:@"bundle"];
NSString*strM = [bundlePathstringByAppendingPathComponent:[NSStringstringWithFormat:@"%@",lastimagestr]];
NSData* imagedata = [[Gamedictiondiccreategamedic].gamedictiondicobjectForKey:[NSStringstringWithFormat:@"%@",lastimagestr]];
if(imagedata==nil) {
imagedata = [NSDatadataWithContentsOfFile:strM];
[[Gamedictiondiccreategamedic].gamedictiondicsetObject:imagedataforKey:[NSStringstringWithFormat:@"%@",lastimagestr]];
}
NSString*mimeType=[NSStringstringWithFormat:@"image/jpg"];
NSURLResponse*response = [[NSURLResponsealloc]initWithURL:self.request.URL
MIMEType:mimeType
expectedContentLength:imagedata.length
textEncodingName:@"UTF8"];
//NSString *dataStr = [[NSString alloc] initWithData:imagedata encoding:NSUTF8StringEncoding];
//
//NSLog(@"%@",dataStr);
[[selfclient]URLProtocol:selfdidReceiveResponse:responsecacheStoragePolicy:NSURLCacheStorageNotAllowed];
[[selfclient]URLProtocol:selfdidLoadData:imagedata];
[[selfclient]URLProtocolDidFinishLoading:self];
NSLog(@"缓存的数据%@",self.request.URL);
}
}
NSMutableURLRequest*mutableReqeust = [[selfrequest]mutableCopy];
//打标签,防止无限循环
[NSURLProtocolsetProperty:@YESforKey:URLProtocolHandledKeyinRequest:mutableReqeust];
self.connection= [NSURLConnectionconnectionWithRequest:mutableReqeustdelegate:self];
}
6.读取完的处理
- (void)stopLoading
{
[self.connectioncancel];
}
7.替换在线数据的代理方法
+(NSMutableURLRequest*)redirectHostInRequset:(NSMutableURLRequest*)request{
};
8.其他代理方法
#pragma mark - NSURLConnectionDelegate
- (void) connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response {
[self.clientURLProtocol:selfdidReceiveResponse:responsecacheStoragePolicy:NSURLCacheStorageNotAllowed];
NSLog(@"拦截的respon%@",response);
}
- (void) connection:(NSURLConnection*)connection didReceiveData:(NSData*)data {
[self.clientURLProtocol:selfdidLoadData:data];
}
- (void) connectionDidFinishLoading:(NSURLConnection*)connection {
[self.clientURLProtocolDidFinishLoading:self];
}
- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error {
[self.clientURLProtocol:selfdidFailWithError:error];
}