user3823935 user3823935 - 3 months ago 19
Swift Question

didReceiveAuthenticationChallenge delegate not getting called in Swift

I am trying a sample project with NSURLConnection.

import Foundation
import UIKit
class loginVC: ViewController, UITextFieldDelegate, NSURLConnectionDelegate, NSURLConnectionDataDelegate{
var webData: NSMutableData!

override func viewDidLoad() {
super.viewDidLoad()
webData = NSMutableData()
callWebService("testdentist@gmail.com", 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)
}
}


It seems all delegates are getting called except didReceiveAuthenticationChallenge delegate method.What i am missing.any help will be appreciated.thanks in advance

Answer

For iOS 8 and above, you must implement connection(_:willSendRequestForAuthenticationChallenge:). connection:didReceiveAuthenticationChallenge: is not called in iOS8, only in older operating systems.

So, to provide authentication in iOS8 and above, implement the method above and in there you must invoke one of the challenge-responder methods (NSURLAuthenticationChallengeSender protocol):

useCredential:forAuthenticationChallenge:

continueWithoutCredentialForAuthenticationChallenge: 

cancelAuthenticationChallenge:

performDefaultHandlingForAuthenticationChallenge: 

rejectProtectionSpaceAndContinueWithChallenge:

didReceiveAuthenticationChallenge is a method on NSURLConnectionDelegate, whilst the rest of your methods (except didFailWithError) are all NSURLConnectionDataDelegate methods. Are you implementing both protocols in your controller? It would perhaps help if you posted all your class' code.

Comments