gpt4 book ai didi

ios - URLsession 委托(delegate)函数出错

转载 作者:行者123 更新时间:2023-11-28 11:17:10 24 4
gpt4 key购买 nike

我正在尝试与 URLSession 委托(delegate)建立 ssl 连接,这是我收到的错误消息:

Objective-C method 'URLSession:didReceiveChallenge:completionHandler:' provided by method 'URLSession(:didReceiveChallenge:completionHandler:)' conflicts with optional requirement method 'URLSession(:didReceiveChallenge:completionHandler:)' in protocol 'NSURLSessionDelegate'

 func URLSession(session: NSURLSession,
didReceiveChallenge challenge:
NSURLAuthenticationChallenge,
completionHandler:
(NSURLSessionAuthChallengeDisposition,
NSURLCredential!) -> Void) {

let serverTrust: SecTrustRef = challenge.protectionSpace.serverTrust!
let serverCert: SecCertificateRef = SecTrustGetCertificateAtIndex(serverTrust, 0).takeUnretainedValue()
let serverKey: NSData = SecCertificateCopyData(serverCert).takeRetainedValue()

let bundle: NSBundle = NSBundle.mainBundle()
let mainbun = bundle.pathForResource("ca", ofType: "der")
let key: NSData = NSData(contentsOfFile: mainbun!)!
// let turntocert: SecCertificateRef =
// SecCertificateCreateWithData(kCFAllocatorDefault, key).takeRetainedValue()

if serverKey == key {

let credential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!)

challenge.sender!.useCredential(credential, forAuthenticationChallenge: challenge)

completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential,credential)

}

else{

challenge.sender!.cancelAuthenticationChallenge(challenge)
completionHandler(NSURLSessionAuthChallengeDisposition.CancelAuthenticationChallenge, nil)

}
}

func URLSession(session: NSURLSession, task: NSURLSessionTask, willPerformHTTPRedirection response: NSHTTPURLResponse, newRequest request: NSURLRequest, completionHandler: (NSURLRequest!) -> Void) {
var newRequest : NSURLRequest? = request
print(newRequest?.description);
completionHandler(newRequest)
}

最佳答案

您现在使用 Swift 2 吗?我在更新到 Swift 2 时开始收到此错误。

我可以通过将其更改为:

func URLSession(session: NSURLSession, 
task: NSURLSessionTask,
didReceiveChallenge challenge: NSURLAuthenticationChallenge,
completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?)
-> Void) {
// your code
}

关于ios - URLsession 委托(delegate)函数出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31069246/

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