【发布时间】: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?