【问题标题】:Swift3: Type 'NSObject' does not conform to protocol 'URLAuthenticationChallengeSender'Swift3:类型“NSObject”不符合协议“URLAuthenticationChallengeSender”
【发布时间】:2016-09-22 10:24:12
【问题描述】:

在 Swift3 中 'NSURLAuthenticationChallengeSender' 更改为 'URLAuthenticationChallengeSender' 所以在委托方法中也从 NSURlAuthentication 更改为 URLAuthentication(Auto Xcode 编辑)

我很困惑我是否仍然缺少任何委托方法,但包括所有委托方法以下仍然显示错误如上。

如果有人遇到同样的问题,请帮忙!!!

Swift3

class GeneralFunctions: NSObject,NSURLConnectionDataDelegate,URLAuthenticationChallengeSender {

}

委托方法

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
}

【问题讨论】:

  • 确保你已经为你的委托方法实现了正确的参数。有些可能会从 error:NSError 变为例如错误:错误?
  • 是的,在 swift3 中有很多这样的变化!!!!再次检查@JVS
  • 它现在是否有可能使用URLConnection,而不是NSURLConnection

标签: ios swift swift3


【解决方案1】:

所以在 Swift 3 中,一些方法的签名略有改变。我也遇到过这个问题。对于您所需的所有委托方法,只需开始输入方法的前几个字母,然后按 ctrl + 空格,它应该会自动完成您的方法的正确签名

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-23
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-22
    相关资源
    最近更新 更多