在用Charles抓包过程中使用KingFisher时遇到如下问题
"The certificate for this server is invalid. You might be connecting to a server that is pretending to be “xxx.xxx.xx” which could put your confidential information at risk."
Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “xxx.xxx.xx” which could put your confidential information at risk." UserInfo={NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFStreamErrorDomainKey=3, NSErrorPeerCertificateChainKey=(
方案一、
首先在模拟器上安装charles的https证书,也就是
1、在模拟器的浏览器里输入:chls.pro/ssl
,然后点击alow
2、之后到模拟器的设置里安装证书(iOS15 系统的入口:Setting-General-Device Management - 选则要安装的证书)
3、信任证书(iOS15 系统的入口:Setting-General-About-Certificate Trust Settings - 选则要信任的证书开启)
解放方案二:
处理特定的域名:
import Kingfisher
let downloader = KingfisherManager.shared.downloader
downloader.trustedHosts = Set(["xxx.xxx.xxx"])
方案三、
写一个类来处理
import Kingfisher
class TrustAllHostAuthenticationChanllengeTool: NSObject, AuthenticationChallengeResponsible {
static let shared = TrustAllHostAuthenticationChanllengeTool()
class func trustAllImageHost() {
let downloader = KingfisherManager.shared.downloader
downloader.authenticationChallengeResponder = TrustAllHostAuthenticationChanllengeTool.shared
}
public func downloader(
_ downloader: ImageDownloader,
didReceive challenge: URLAuthenticationChallenge,
completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void)
{
let credential = URLCredential(trust: challenge.protectionSpace.serverTrust!)
completionHandler(.useCredential, credential)
return
}
}
// 在appdelegate的finishlaunch里调用一下
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
TrustAllHostAuthenticationChanllengeTool.trustAllImageHost()
return true
}
// other
}
网友评论