didReceiveAuthenticationChallenge委托没有在Swift中调用
我正在尝试使用NSURLConnection的示例项目。didReceiveAuthenticationChallenge委托没有在Swift中调用
import Foundation
import UIKit
class loginVC: ViewController, UITextFieldDelegate, NSURLConnectionDelegate, NSURLConnectionDataDelegate{
var webData: NSMutableData!
override func viewDidLoad() {
super.viewDidLoad()
webData = NSMutableData()
callWebService("[email protected]", Password:"1")
}
func callWebService(userName:NSString, Password:NSString){
var strURl: NSURL = NSURL .URLWithString("")
var request: NSMutableURLRequest = NSMutableURLRequest(URL: strURl, cachePolicy:NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData, timeoutInterval:60.0)
var postString: NSString = ""
postString = postString.stringByAppendingFormat("username=%@&password=%@", userName,Password)
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
request.HTTPMethod = ""
var connection: NSURLConnection = NSURLConnection(request: request, delegate:self)
connection.start()
}
//NSURLConnection webservice
func connection(connection: NSURLConnection!, didReceiveResponse response: NSURLResponse!){
// webData.length = 0
println("response")
}
func connection(connection: NSURLConnection!, didReceiveData data: NSData!){
println(data.length)
webData .appendData(data)
}
func connection(connection: NSURLConnection, didFailWithError error: NSError!){
println("error in connection")
}
func connectionDidFinishLoading(connection: NSURLConnection!){
var response: NSString = NSString(data: webData, encoding: NSUTF8StringEncoding)
println("response:\(response)")
if response != ""{
}
}
func connection(connection: NSURLConnection, didReceiveAuthenticationChallenge challenge: NSURLAuthenticationChallenge!){
var authentication: NSURLCredential = NSURLCredential.credentialWithUser("", password:"", persistence: NSURLCredentialPersistence.ForSession)
}
}
似乎所有代表都获取调用除了didReceiveAuthenticationChallenge代表method.What我是missing.any帮助将appreciated.thanks提前
对于iOS 8.0及以上,必须实现connection(_:willSendRequestForAuthenticationChallenge:)
。在iOS8中不会调用connection:didReceiveAuthenticationChallenge:
,只能在较早的操作系统中调用。
所以,在iOS8上提供认证及以上,实现上面的方法,并在那里你必须调用的挑战 - 应答方法之一(NSURLAuthenticationChallengeSender protocol
):
useCredential:forAuthenticationChallenge:
continueWithoutCredentialForAuthenticationChallenge:
cancelAuthenticationChallenge:
performDefaultHandlingForAuthenticationChallenge:
rejectProtectionSpaceAndContinueWithChallenge:
didReceiveAuthenticationChallenge
是方法上NSURLConnectionDelegate
,而其余的方法(didFailWithError
除外)均为NSURLConnectionDataDelegate
方法。你是否在你的控制器中实现了两种协议?如果你发布了所有班级的代码,这可能会有所帮助。
我已经实现了两个协议,但它没有被调用? – user3823935 2014-08-28 08:40:47
请显示你的全班。 – KerrM 2014-08-28 08:42:25
我更新了我的代码.. – user3823935 2014-08-28 08:51:05
我不认为这个问题是在这些方面新雨燕3.0 ..
我有类似的问题,一个代码下xcode7/ios7-8-9 /雨燕2.2
定期工作迁移已经产生:
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: (Foundation.URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
... }
但不工作。
重写手:
func urlSession(_: URLSession,
didReceive challenge: URLAuthenticationChallenge,
completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
}
现在它的工作。 我的两毛钱。
您不需要在连接上调用start() - 只有在使用'initWithRequest:delegate:startImmediately:'并传递false来立即启动时才需要'start()'。 – KerrM 2014-08-28 08:58:49