NSURLProtocal子类的使用

Posted tiankongzhicheng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NSURLProtocal子类的使用相关的知识,希望对你有一定的参考价值。

//

//  CustomURLProtocol.m

//  NSURLProtocolExample

//

//  Created by lujb on 15/6/15.

//  Copyright (c) 2015年 lujb. All rights reserved.

//

 

#import "CustomURLProtocol.h"

 

static NSString * const URLProtocolHandledKey = @"URLProtocolHandledKey";

 

@interface CustomURLProtocol ()<NSURLConnectionDelegate>

 

@property (nonatomic, strong) NSURLConnection *connection;

 

@end

 

@implementation CustomURLProtocol

 

+ (BOOL)canInitWithRequest:(NSURLRequest *)request

{

    //只处理http和https请求

    NSString *scheme = [[request URL] scheme];

    if ( ([scheme caseInsensitiveCompare:@"http"] == NSOrderedSame ||

     [scheme caseInsensitiveCompare:@"https"] == NSOrderedSame))

    {

        //看看是否已经处理过了,防止无限循环

        if ([NSURLProtocol propertyForKey:URLProtocolHandledKey inRequest:request]) {

            return NO;

        }

        

        return YES;

    }

    return NO;

}

 

+ (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request {

    NSMutableURLRequest *mutableReqeust = [request mutableCopy];

    mutableReqeust = [self redirectHostInRequset:mutableReqeust];

    

    

    return [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com"]];

}

 

+ (BOOL)requestIsCacheEquivalent:(NSURLRequest *)a toRequest:(NSURLRequest *)b

{

    return [super requestIsCacheEquivalent:a toRequest:b];

}

 

- (void)startLoading

{

    /* 如果想直接返回缓存的结果,构建一个NSURLResponse对象

    if (cachedResponse) {

        

        NSData *data = cachedResponse.data; //缓存的数据

        NSString *mimeType = cachedResponse.mimeType;

        NSString *encoding = cachedResponse.encoding;

        

        NSURLResponse *response = [[NSURLResponse alloc] initWithURL:self.request.URL

                                                            MIMEType:mimeType

                                               expectedContentLength:data.length

                                                    textEncodingName:encoding];

        

        [self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];

        [self.client URLProtocol:self didLoadData:data];

        [self.client URLProtocolDidFinishLoading:self];

    */

    

    NSMutableURLRequest *mutableReqeust = [[self request] mutableCopy];

    

    //打标签,防止无限循环

    [NSURLProtocol setProperty:@YES forKey:URLProtocolHandledKey inRequest:mutableReqeust];

    

    self.connection = [NSURLConnection connectionWithRequest:mutableReqeust delegate:self];

}

 

- (void)stopLoading

{

    [self.connection cancel];

}

 

#pragma mark - NSURLConnectionDelegate

 

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

    [self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];

}

 

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

    [self.client URLProtocol:self didLoadData:data];

}

 

- (void) connectionDidFinishLoading:(NSURLConnection *)connection {

    [self.client URLProtocolDidFinishLoading:self];

}

 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

    [self.client URLProtocol:self didFailWithError:error];

}

 

#pragma mark -- private

 

+(NSMutableURLRequest*)redirectHostInRequset:(NSMutableURLRequest*)request

{

    if ([request.URL host].length == 0) {

        return request;

    }

    

    NSString *originUrlString = [request.URL absoluteString];

    NSString *originHostString = [request.URL host];

    NSRange hostRange = [originUrlString rangeOfString:originHostString];

    if (hostRange.location == NSNotFound) {

        return request;

    }

    

    //定向到bing搜索主页

    NSString *ip = @"cn.bing.com";

    

    // 替换host

    NSString *urlString = [originUrlString stringByReplacingCharactersInRange:hostRange withString:ip];

    NSURL *url = [NSURL URLWithString:urlString];

    request.URL = url;

 

    return request;

}

@end

 

以上是关于NSURLProtocal子类的使用的主要内容,如果未能解决你的问题,请参考以下文章

使用“this”调用超类方法的子类

子类要调用父类的方法,必须使用super关键字.对吗?

在java中子类若要继承父类,需要使用的关键字是啥

java中父类与子类的关系以及使用

子类可以使用超类Ios的委托方法吗

iOS:使用 nib 子类化 UITableViewCell,进入 UIView(不是视图控制器)子类