1. 使用自签名证书,https请求时,发现SSL 花样报错
swift
语言中
1.1 第三方网络请求:Alamofire
1.1.1 自定义EKWSessionManager
继承于SessionManager
,在Debug模式下添加需要添加信任的host, 如:www.jianshu.com
, www.baidu.com
import UIKit
import Alamofire
class EKWSessionManager: SessionManager {
open static let defaultManager: SessionManager = {
#if DEBUG
let configuration = URLSessionConfiguration.default
//设置需要信任的host
let serverTrustPolicies: [String : ServerTrustPolicy] = ["www.baidu.com": .disableEvaluation, "www.jianshu.com": .disableEvaluation]
let sessionManager = SessionManager.init(configuration: configuration, serverTrustPolicyManager: ServerTrustPolicyManager.init(policies: serverTrustPolicies))
#else
let sessionManager = SessionManager.default
#endif
return sessionManager
}()
}
1.1.2 接下来的网络请求都是基于EKWSessionManager. defaultManager
let sessionManager = EKWSessionManager.defaultManager
let dataRequest = sessionManager.request(url,
method: method,
parameters: paramenters,
encoding: URLEncoding.default,
headers: nil)
1.2 第三方图片加载:Kingfisher
在Debug模式下添加需要添加信任的host, 如:www.jianshu.com
, www.baidu.com
class func serverTrustPolicy(){
#if DEBUG
let downloader = KingfisherManager.shared.downloader
downloader.trustedHosts = ["www.jianshu.com", "www.baidu.com"]
#else
#endif
}
OC
语言中
1.3 使用OC语言,在Debug模式下添加绕过证书验证的方法
#ifdef DEBUG
@implementation NSURLRequest(DataController)
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host
{
return YES;
}
@end
#endif