在 Swift3 中,“NSURLAuthenticationChallengeSender”更改为“URLAuthenticationChallengeSender”,因此委托(delegate)方法中也从 NSURlAuthentication 更改为 URLAuthentication(自动 Xcode 编辑)
我很困惑我是否还缺少任何委托(delegate)方法,但包含了以下所有委托(delegate)方法,但它仍然显示如上所述的错误。
如果有人遇到同样的问题,请帮忙解决!!!
swift 3
class GeneralFunctions: NSObject,NSURLConnectionDataDelegate,URLAuthenticationChallengeSender {
}
委托(delegate)方法
func connection(connection: NSURLConnection, willSendRequestForAuthenticationChallenge challenge: URLAuthenticationChallenge) {
}
func connection(connection: NSURLConnection, didReceiveResponse:URLResponse)
{
}
func connection(connection: NSURLConnection, didReceiveData data: NSData) {
}
func connection(connection: NSURLConnection, didFailWithError error: NSError) {
print("Connection Failed")
}
func connection(connection: NSURLConnection, didReceiveAuthenticationChallenge challenge: URLAuthenticationChallenge) {
//sanju swift3 160922-- re verify
// let myCredential = URLCredential(forTrust: challenge.protectionSpace.serverTrust!)
// challenge.sender!.useCredential(myCredential, forAuthenticationChallenge: challenge)
}
func connection(connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: URLProtectionSpace) -> Bool {
return true
}
func URLSession(session: URLSession, didReceiveChallenge challenge: URLAuthenticationChallenge, completionHandler: (URLSession.AuthChallengeDisposition, URLCredential) -> Void) {
let protectionSpace = challenge.protectionSpace
let theSender = challenge.sender
if protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
if let theTrust = protectionSpace.serverTrust{
let theCredential = URLCredential(trust: theTrust)
theSender?.use(theCredential, for: challenge)
return
}
}
theSender?.performDefaultHandling!(for: challenge)
return
}
最佳答案
因此,在 Swift 3 中,某些方法的签名略有变化。我也面临这个问题。对于所有所需的委托(delegate)方法,只需开始输入方法的前几个字母,然后按 ctrl + 空格键,它就会自动完成方法的正确签名
关于ios - Swift3:类型 'NSObject' 不符合协议(protocol) 'URLAuthenticationChallengeSender',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39636518/