Skip to content

Instantly share code, notes, and snippets.

@joshavant
Last active July 1, 2016 15:21
Show Gist options
  • Save joshavant/196dd41d62783b5fd0f7751027dec699 to your computer and use it in GitHub Desktop.
Save joshavant/196dd41d62783b5fd0f7751027dec699 to your computer and use it in GitHub Desktop.
// 1. Implement URLSession:task:didReceiveChallenge:completionHandler:. (Be sure to call the completion handler with NSURLSessionAuthChallengeDisposition.PerformDefaultHandling and a nil NSURLCredential.)
// 2. Set a breakpoint in the callback + trigger an authentication challenge at runtime
// 3. In the debugger, inspect the callback's `challenge.protectionSpace` argument for the values to put in the following lines of code...
// 4. Put the following lines of code somewhere before your network requests (i.e. AppDelegate area, or other)
let basicAuthCredentials = NSURLCredential(user: "username", password: "password", persistence: .Permanent)
let foobarHttpsProtectionSpace = NSURLProtectionSpace(host: "foo.bar.com", port: 443, protocol: "https", realm: "This Probably Has A Value, Get It From The Delegate Callback", authenticationMethod: NSURLAuthenticationMethodHTTPBasic)
NSURLCredentialStorage.sharedCredentialStorage().setDefaultCredential(basicAuthCredentials, forProtectionSpace: apidevHttpsProtectionSpace)
// 5. Remove the delegate callback implementation. The credentials stored in the `sharedCredentialStorage` will be used, when required.
@algal
Copy link

algal commented Jul 1, 2016

I feel like I've found a long-buried treasure!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment