貌似在iOS9之后纳猫,官方默認只能訪問https披诗。如果訪問的https都是安全的,則不需要做任何配置囱挑。
安全的https都是通過官方認可的機構購買的SSL證書萌丈,如果是自己配置的證書則需要做一下配置了瘟忱。
1耐朴、SSL證書
向后臺開發(fā)者獲取SSL證書(crt格式)幢炸,并將該文件的格式轉換成cer格式:
- 方式一:個人試過充岛,報錯了
$ cd /Users/thbdsz/Desktop/證書/
$ openssl x509 -in m2.crt -out zj.cer -outform der
unable to load certificate
140735677465544:error:0906D06C:PEM routines:PEM_read_bio:no start line:/BuildRoot/Library/Caches/com.apple.xbs/Sources/libressl/libressl-22.50.2/libressl/crypto/pem/pem_lib.c:704:Expecting: TRUSTED CERTIFICATE
-
方式二:雙擊該文件保檐,在 keychain (鑰匙串訪問)中找到該文件的證書,然后導出cer文件崔梗。注意夜只,完成后,在keychain中刪除該證書蒜魄。不然通過該電腦訪問與該證書相同域名的url時扔亥,都需要配置該證書。
ssl證書.png
2谈为、AFN 3.0的訪問
注意旅挤,這里老的的版本(具體什么版本不知道)可以不配置 kApiBaseUrl
,但是新的版本必須配置伞鲫,并通過 kApiBaseUrl
創(chuàng)建 AFHTTPSessionManager
粘茄。
@interface NetManager : AFHTTPSessionManager
+ (instancetype)share;
@end
@implementation NetManager
+ (instancetype)share{
static NetManager *manager;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
manager = [[self alloc] initWithBaseURL:[NSURL URLWithString:kApiBaseUrl] sessionConfiguration:configuration];
[manager setSecurityPolicy:[self customSecurityPolicy]];
});
return manager;
}
+ (AFSecurityPolicy *)customSecurityPolicy {
// 先導入證書 證書由服務端生成,具體由服務端人員操作
NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"m2" ofType:@"cer"];//證書的路徑 xx.cer
NSData *cerData = [NSData dataWithContentsOfFile:cerPath];
// AFSSLPinningModeCertificate 使用證書驗證模式
AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
// allowInvalidCertificates 是否允許無效證書(也就是自建的證書)榔昔,默認為NO
// 如果是需要驗證自建證書驹闰,需要設置為YES
securityPolicy.allowInvalidCertificates = YES;
//validatesDomainName 是否需要驗證域名,默認為YES;
//假如證書的域名與你請求的域名不一致撒会,需把該項設置為NO嘹朗;如設成NO的話,即服務器使用其他可信任機構頒發(fā)的證書诵肛,也可以建立連接屹培,這個非常危險,建議打開怔檩。
//置為NO褪秀,主要用于這種情況:客戶端請求的是子域名,而證書上的是另外一個域名薛训。因為SSL證書上的域名是獨立的媒吗,假如證書上注冊的域名是www.google.com,那么mail.google.com是無法驗證通過的乙埃;當然闸英,有錢可以注冊通配符的域名*.google.com锯岖,但這個還是比較貴的。
//如置為NO甫何,建議自己添加對應域名的校驗邏輯出吹。
securityPolicy.validatesDomainName = NO;
securityPolicy.pinnedCertificates = [[NSSet alloc] initWithObjects:cerData, nil];
return securityPolicy;
}
@end
3、info.plist辙喂,配置白名單
<dict>
<key>NSAllowsArbitraryLoads</key>
<false/>
<key>NSExceptionDomains</key>
<dict>
<key>cer文件中查看</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSExceptionMinimumTLSVersion</key>
<string>TLSv1.0</string>
<key>NSExceptionRequiresForwardSecrecy</key>
<false/>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
</dict>
</dict>
cer.png
不配置白名單捶牢,照樣出錯:
[BoringSSL] Function nw_protocol_boringssl_input_finished: line 1436 Peer disconnected during the middle of a handshake. Sending errSSLFatalAlert(-9802) alert
TIC TCP Conn Failed [1:0x608000361ec0]: 3:-9802 Err(-9802)
NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)
Task <B02837F8-D481-46BF-9A1B-2F76E68B1A04>.<1> HTTP load failed (error code: -1200 [3:-9802])
Task <B02837F8-D481-46BF-9A1B-2F76E68B1A04>.<1> finished with error - code: -1200
2018-06-01 16:15:07.873601+0800 Test[26837:1632515] Error Domain=NSURLErrorDomain Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made."
...等等信息
4、SDWebImage
使用下面方法即可巍耗。
[imv sd_setImageWithURL:[NSURL URLWithString:@"https://xxxxx:8080/img/upload/xxx.png"] placeholderImage:nil options:SDWebImageAllowInvalidSSLCertificates];
5秋麸、WKWebView
設置代理,并實現(xiàn)下面的方法芍锦。
_webView.navigationDelegate = self;
- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler {
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
NSURLCredential *card = [[NSURLCredential alloc] initWithTrust:challenge.protectionSpace.serverTrust];
completionHandler(NSURLSessionAuthChallengeUseCredential,card);
}
}
6竹勉、其他
如果項目中因為某些原因不全是https環(huán)境,例如分享功能娄琉,也需要添加白名單。
詳情可看mob上說的適配iOS9.0吓歇。
如果項目中還需要使用不知道域名為啥的http孽水,則修改info.plist
<key>NSAllowsArbitraryLoads</key>
<true/>