gpt4 book ai didi

ios - iOS 12 上的 NSAppTransportSecurity + NSAllowsArbitraryLoads

转载 作者:行者123 更新时间:2023-11-29 05:55:54 27 4
gpt4 key购买 nike

曾经有一种方法可以解决 iOS 对具有有效证书的 HTTP 的坚持。我对在这种状态下将应用程序提交到商店不感兴趣,我只想在开发应用程序时与 Charles 一起嗅探网络操作。

谢谢

我试过了

<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>

...以及网上常见的所有其他变体。

一定有办法...

最佳答案

您可以使用 URLSessionRequest 将以下代码用于 SSL 请求,

 fileprivate func SSLCertificateCreateTrustResult(_ serverTrust: SecTrust)->SecTrustResultType {
let certificate: SecCertificate = SecTrustGetCertificateAtIndex(serverTrust, 0)!
let remoteCertificateData = CFBridgingRetain(SecCertificateCopyData(certificate))!
var certName = "certName"

let cerPath: String = Bundle.main.path(forResource: certName, ofType: "der")!
let localCertificateData = NSData(contentsOfFile:cerPath)!

let certDataRef = localCertificateData as CFData
let cert = (SecCertificateCreateWithData(nil, certDataRef))
let certArrayRef = [cert] as CFArray
SecTrustSetAnchorCertificates(serverTrust, certArrayRef)
SecTrustSetAnchorCertificatesOnly(serverTrust, false)
let trustResult: SecTrustResultType = SecTrustResultType.invalid
return trustResult
}
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
if challenge.protectionSpace.authenticationMethod == (NSURLAuthenticationMethodServerTrust) {
let serverTrust:SecTrust = challenge.protectionSpace.serverTrust!
var localCertificateTrust = SSLCertificateCreateTrustResult(serverTrust)
SecTrustEvaluate(serverTrust, &localCertificateTrust)
if localCertificateTrust == SecTrustResultType.unspecified || localCertificateTrust == SecTrustResultType.proceed || localCertificateTrust == SecTrustResultType.recoverableTrustFailure
{
let credential:URLCredential = URLCredential(trust: serverTrust)
challenge.sender?.use(credential, for: challenge)
completionHandler(URLSession.AuthChallengeDisposition.useCredential, URLCredential(trust: challenge.protectionSpace.serverTrust!))

} else {
let properties = SecTrustCopyProperties(serverTrust)
completionHandler(URLSession.AuthChallengeDisposition.cancelAuthenticationChallenge, nil)
}
}
else
{
completionHandler(URLSession.AuthChallengeDisposition.cancelAuthenticationChallenge, nil);
}
}

状况良好

if localCertificateTrust == SecTrustResultType.unspecified || localCertificateTrust == SecTrustResultType.proceed || localCertificateTrust == SecTrustResultType.recoverableTrustFailure

以下类型适用于有效证书

SecTrustResultType.unspecified , SecTrustResultType.proceed

对于无效证书SecTrustResultType.recoverableTrustFailure

我已在 || 条件下添加了上述所有三个证书,以便在您想要删除任何可以删除其他类型的证书时使用有效和无效的证书

关于ios - iOS 12 上的 NSAppTransportSecurity + NSAllowsArbitraryLoads,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55161939/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com