Q:

Swift and Alamofire SSL Pinning and Handling Challenge

I need to handle the challenge when I try to get the json from my internal web server.  I followed this from a previous question.  Here is my code 
let defaultManager: Alamofire.SessionManager = {
            let serverTrustPolicies: [String: ServerTrustPolicy] = [
                "myhttpsinternaldomain.org": .disableEvaluation
            ]

            let configuration = URLSessionConfiguration.default
            configuration.httpAdditionalHeaders = Alamofire.SessionManager.defaultHTTPHeaders

            return Alamofire.SessionManager(
                configuration: configuration,
                serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
            )
        }()

        let url = URL(string: urlString)
        let username = "user"
        let password = "password"
        let header = ["user": username, "password": password]

        defaultManager.request(url!, method: .get, headers: header).responseJSON { response in
            switch response.result {
            case .success(let value):
                let json = JSON(value)
                print("JSON: \(json)")
            case .failure(let error):
                print(error)
            }
        }

This is the error I receive

Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo={NSErrorFailingURLKey=https://myhttpsinternaldomain.org, NSLocalizedDescription=cancelled, NSErrorFailingURLStringKey=https://myhttpsinternaldomain.org}

I found this but I don't have the tokens in my project.  I want to just user the username and password or ignore the challenge
Please any help

A:

Got it! Or rather I found the right page to that led me to the answer.  Github Page Server Trust Policy Manager and connect to self signed servers using Alamofire 
Originally I would get a 310 error, after the SSL would deny my authentication.
Then I added the manager class I posted originally and received a -999 error saying that it was "cancelled".  Thanks to the Github Page above the reason is because I needed to

"Make sure to keep a reference to the new SessionManager instance,
  otherwise your requests will all get cancelled when your
  sessionManager is deallocated."

So I created a NetworkManager class thanks to the second stackoverflow page, and called it in my alamofire request.  
Here is the code below that works,  hopefully this will save someone a lot of time.
// Network Manager outside view controller

        class NetworkManager {
        static let sharedInstance = NetworkManager()

        let manager: Alamofire.SessionManager = {
            let serverTrustPolicies: [String: ServerTrustPolicy] = [
                "https://mydomainthatwasdrivingmebananas.com": .pinCertificates(
                    certificates: ServerTrustPolicy.certificates(),
                    validateCertificateChain: true,
                    validateHost: true),
                "mydomainthatwasdrivingmebananas.com": .disableEvaluation
            ]

            let configuration = URLSessionConfiguration.default
            configuration.httpAdditionalHeaders = Alamofire.SessionManager.defaultHTTPHeaders

            return Alamofire.SessionManager(
                configuration: configuration,
                serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
            )
        }()
    }

Then the Alamofire call
// Inside ViewController ViewDidLoad

NetworkManager.sharedInstance.manager.request("https://mydomainthatwasdrivingmebananas.com", method: .get).responseJSON{ response in
                switch response.result {
                                case .success(let value):
                                    let json = JSON(value)
                                    print("JSON: \(json)")
                                case .failure(let error):
                                    print(error)
                                }

        }

P.S the JSON() method is swifty JSON in case anyone was confused

