我正在嘗試使用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提前
您不需要在連接上調用start() - 只有在使用'initWithRequest:delegate:startImmediately:'並傳遞false來立即啓動時才需要'start()'。 – KerrM 2014-08-28 08:58:49