最近App彷佛有报异常是DNS没法解析,尝试解决此问题.搜集到的资料不多,甚至连AFN原做者都断定这多是一个无解的问题,参见: https://github.com/AFNetworking/AFNetworking/issues/2954,不过最终仍是靠着stackoverflow上的一丁点提示,顺利找到并聚集成了一个可用的解决方案.大喜,与君共享!html
经过IP直接访问网站,能够解决DNS劫持问题.DNS劫持,能够经过修改电脑的host文件模拟.若是是HTTP请求,使用ip地址直接访问接口,配合header中Host字段带上原来的域名信息便可;若是是 https请求,会很麻烦,须要 Overriding TLS Chain Validation Correctly
;curl 中有一个 -resolve
方法能够实现使用指定ip访问https网站,iOS中集成curl库应该也能够,不过改动太大,未验证;对于服务器IP常常变的状况,可能须要使用httpDNS服务,参见:https://www.dnspod.cn/httpdns.ios
在Info.plist中添加NSAppTransportSecurity类型Dictionary,在NSAppTransportSecurity下添加NSAllowsArbitraryLoads类型Boolean,值设为YES.这些原本是用来解决iOS9下,容许HTTP请求访问网络的,固然做用不止这些.具体缘由感兴趣的自行google.git
给 AFURLConnectionOperation 类添加新属性:github
/** 可信任的域名,用于支持经过ip访问此域名下的https连接. Trusted domain, this domain for support via IP access HTTPS links. */ @property(nonatomic, strong) NSMutableArray * trustHostnames;
给 AFURLConnectionOperation 实现的代理方法: - (void)connection:(NSURLConnection *)connection
willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge 添加添加可信任的域名的相关逻辑代码:服务器
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { if (self.authenticationChallenge) { self.authenticationChallenge(connection, challenge); return; } if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) { SecTrustRef serverTrust = challenge.protectionSpace.serverTrust; /* 添加可信任的域名,以支持:直接使用ip访问特定https服务器. Add trusted domain name to support: direct use of IP access specific HTTPS server.*/ for (NSString * trustHostname in [self trustHostnames]) { serverTrust = AFChangeHostForTrust(serverTrust, trustHostname); } ....
参考Apple官方文档,实现自定义的添加可信域名的函数: AFChangeHostForTrust网络
static inline SecTrustRef AFChangeHostForTrust(SecTrustRef trust, NSString * trustHostname) { if ( ! trustHostname || [trustHostname isEqualToString:@""]) { return trust; } CFMutableArrayRef newTrustPolicies = CFArrayCreateMutable( kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks); SecPolicyRef sslPolicy = SecPolicyCreateSSL(true, (CFStringRef)trustHostname); CFArrayAppendValue(newTrustPolicies, sslPolicy); #ifdef MAC_BACKWARDS_COMPATIBILITY /* This technique works in OS X (v10.5 and later) */ SecTrustSetPolicies(trust, newTrustPolicies); CFRelease(oldTrustPolicies); return trust; #else /* This technique works in iOS 2 and later, or OS X v10.7 and later */ CFMutableArrayRef certificates = CFArrayCreateMutable( kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks); /* Copy the certificates from the original trust object */ CFIndex count = SecTrustGetCertificateCount(trust); CFIndex i=0; for (i = 0; i < count; i++) { SecCertificateRef item = SecTrustGetCertificateAtIndex(trust, i); CFArrayAppendValue(certificates, item); } /* Create a new trust object */ SecTrustRef newtrust = NULL; if (SecTrustCreateWithCertificates(certificates, newTrustPolicies, &newtrust) != errSecSuccess) { /* Probably a good spot to log something. */ return NULL; } return newtrust; #endif }
使用AOP方法,重写 AFURLConnectionOperation 的trustHostnames属性:app
/* 使用AOP方式,指定可信任的域名, 以支持:直接使用ip访问特定https服务器.*/ [AFURLConnectionOperation aspect_hookSelector:@selector(trustHostnames) withOptions:AspectPositionInstead usingBlock: ^(id<AspectInfo> info){ __autoreleasing NSArray * trustHostnames = @[@"www.example.com"]; NSInvocation *invocation = info.originalInvocation; [invocation setReturnValue:&trustHostnames]; }error:NULL];
此处用到的是一个 iOS AOP库,不熟悉的点这里: http://www.ios122.com/2015/08/aspects/.dom