ios - Swift3:类型 'NSObject' 不符合协议(protocol) 'URLAuthenticationChallengeSender'

标签 ios swift swift3

在 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/

相关文章:

ios - NSOperationQueue 中的 NSOperation 未被执行

ios - UIImageView 的活力效果

ios - 始终保持应用程序打开的专用 API

ios - 如何将自定义结构数组保存到 plist swift

swift - 线程 1 exc_bad_instruction.. 快速错误

ios - UITableViewCell 滚动检测

ios - 仅在按下 uitableviewcell 时显示 View

javascript - 从 Swift 更新 Javascript 值

ios - 台风参数注入(inject)与初始化器崩溃应用程序

swift 3 : can't set optional Bool via reflection