Ng Yong Ming
Ng Yong Ming

Reputation: 1

Error with URLsession delegate function

I am trying to make a ssl connection with URLSession delegate and this is the error message I get:

Objective-C method 'URLSession:didReceiveChallenge:completionHandler:' provided by method 'URLSession(:didReceiveChallenge:completionHandler:)' conflicts with optional requirement method 'URLSession(:didReceiveChallenge:completionHandler:)' in protocol 'NSURLSessionDelegate'

 func URLSession(session: NSURLSession,
    didReceiveChallenge challenge:
    NSURLAuthenticationChallenge,
    completionHandler:
    (NSURLSessionAuthChallengeDisposition,
    NSURLCredential!) -> Void) {

        let serverTrust: SecTrustRef = challenge.protectionSpace.serverTrust!
        let serverCert: SecCertificateRef = SecTrustGetCertificateAtIndex(serverTrust, 0).takeUnretainedValue()
        let serverKey: NSData = SecCertificateCopyData(serverCert).takeRetainedValue()

        let bundle: NSBundle = NSBundle.mainBundle()
        let mainbun = bundle.pathForResource("ca", ofType: "der")
        let key: NSData = NSData(contentsOfFile: mainbun!)!
     //   let turntocert: SecCertificateRef =
      //  SecCertificateCreateWithData(kCFAllocatorDefault, key).takeRetainedValue()

        if serverKey == key {

            let credential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!)

            challenge.sender!.useCredential(credential, forAuthenticationChallenge: challenge)

            completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential,credential)

        }

        else{

            challenge.sender!.cancelAuthenticationChallenge(challenge)
            completionHandler(NSURLSessionAuthChallengeDisposition.CancelAuthenticationChallenge, nil)

        }
}

func URLSession(session: NSURLSession, task: NSURLSessionTask, willPerformHTTPRedirection response: NSHTTPURLResponse, newRequest request: NSURLRequest, completionHandler: (NSURLRequest!) -> Void) {
    var newRequest : NSURLRequest? = request
    print(newRequest?.description);
    completionHandler(newRequest)
}

Upvotes: 0

Views: 1901

Answers (2)

Jonathan T
Jonathan T

Reputation: 13

The correct method signature is:

func URLSession(session: NSURLSession,
                didReceiveChallenge challenge: NSURLAuthenticationChallenge,
                completionHandler: (NSURLSessionAuthChallengeDisposition,
                                  NSURLCredential?) -> Void)

where the NSURLCredential parameter MUST be optional (with the question mark (?) after the optional value).

See apple documentation.

Upvotes: 1

JoyA
JoyA

Reputation: 101

Do you use Swift 2 now? I started getting this error when updating to Swift 2.

I could solve this by changing it to:

func URLSession(session: NSURLSession, 
                task: NSURLSessionTask, 
                didReceiveChallenge challenge: NSURLAuthenticationChallenge,  
                completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?) 
                -> Void) {
            // your code
}

Upvotes: 2

Related Questions