首先,本地保存一份初始的DNS Json文件
NSString *originUrlString = [request.URL absoluteString];
NSString *originHostString = [request.URL host];
NSRange hostRange = [originUrlString rangeOfString:originHostString];
if (hostRange.location != NSNotFound) {
NSString *ip = [[CustomDNSManager sharedClient] requestIPWithHost:originHostString];
}
若要修改,修改RichURLSessionProtocol中的canonicalRequestForRequest方法
不包含单独的session和AFN
-(BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[NSURLProtocol registerClass:[RichURLSessionProtocol class]];
}
期间的接口请求也会被拦截 页面初始化时
- (void)viewDidLoad {
[super viewDidLoad];
[NSURLProtocol registerClass:[RichURLSessionProtocol class]];
}
页面销毁
- (void)dealloc {
[NSURLProtocol unregisterClass:[RichURLSessionProtocol class]];
}
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSArray *protocolClasses = @[[RichURLSessionProtocol class]];
config.protocolClasses = protocolClasses;
AFHTTPSessionManager *manage = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:config];
- [Github:NSURLProtocol-WebKitSupport] (https://github.com/Yeatse/NSURLProtocol-WebKitSupport)
- [可能是最全的iOS端HttpDns集成方案] (https://www.jianshu.com/p/cd4c1bf1fd5f)
- NSURLPtotocol无法拦截AFN ,WKWebView