This is a wrinkle on the regular NSURLSession completion block problem, which I'm having a heck of a time resolving into Swift syntax. The method is the authentication delegate callback, which is invoked on auth challenge; the developer calls the completion block with NSURLCredential, NSError.
The Objective-C method looks like this:
-(void) provideUsernamePasswordForAuthChallenge:(NSURLAuthenticationChallenge *)authChallenge completionHandler:(void (^)(NSURLCredential *, NSError *))completionHandler{
NSURLCredential* credential = [NSURLCredential credentialWithUser:@"myname"
password:@"mypass"
persistence:NSURLCredentialPersistenceForSession];
completionHandler(credential, nil);
}
The closest I think I've gotten in Swift, is this:
func provideUsernamePasswordForAuthChallenge(authChallenge: NSURLAuthenticationChallenge!, completionHandler:(() -> (NSURLCredential?, NSError?)) {
var cred = NSURLCredential(user: "myname", password: "mypass", persistence: NSURLCredentialPersistence.ForSession)
return (cred, nil)
})
But, it's still barfing. Any recommendations?
You need to call completionHandler with (cred, nil), not return a tuple
func provideUsernamePasswordForAuthChallenge(authChallenge: NSURLAuthenticationChallenge!, completionHandler:(NSURLCredential?, NSError?)->()) ->() {
var cred =
NSURLCredential(user: "myname", password: "mypass",
persistence: NSURLCredentialPersistence.ForSession)
completionHandler(cred, nil)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With