Group Group Group Group Group Group Group Group Group

Alamofire error on SSL certificate

Hi Everyone,

I’d need some help here, I am stuck with this error as below. Thanks!!
My app has SSL pinning which worked the whole time. And now I need to update the new SSL and domain as the server has changed.

I went in and replace cert/ code change that is related to the cert. But it’s still showing me this error. It should just be a simple change.

FAILURE: Error Domain=NSURLErrorDomain Code=-999 “cancelled” UserInfo={NSErrorFailingURLStringKey=https://website/DVSAPI_PROCESS_LOGIN?, NSErrorFailingURLKey=https://website/DVSAPI_PROCESS_LOGIN?, _NSURLErrorRelatedURLSessionTaskErrorKey=(

"LocalDataTask .<2>"

*), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask .<2>, NSLocalizedDescription=cancelled}

I modified my code like below and it worked. I referred Swift: How to Make Https Request Using Server SSL Certificate for fixing this issue.

   class LoginService{
         private static var Manager: Alamofire.SessionManager = {

              // Create the server trust policies
              let serverTrustPolicies: [String: ServerTrustPolicy] = [

                   "devportal:8443": .disableEvaluation
              ]

              // Create custom manager
              let configuration = URLSessionConfiguration.default
              configuration.httpAdditionalHeaders = Alamofire.SessionManager.defaultHTTPHeaders
              let manager = Alamofire.SessionManager(
                   configuration: URLSessionConfiguration.default,
                   serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
              )

              return manager
         }()



         /**
          Calls the Login Web Service to authenticate the user
          */
         public func login(username:String, password: String){

// Handle Authentication challenge

      let delegate: Alamofire.SessionDelegate = LoginService.Manager.delegate
     delegate.sessionDidReceiveChallenge = { session, challenge in
          var disposition: URLSession.AuthChallengeDisposition = .performDefaultHandling
          var credential: URLCredential?
          if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
               disposition = URLSession.AuthChallengeDisposition.useCredential
               credential = URLCredential(trust: challenge.protectionSpace.serverTrust!)
          } else {
               if challenge.previousFailureCount > 0 {
                    disposition = .cancelAuthenticationChallenge
               } else {
                    credential = LoginService.Manager.session.configuration.urlCredentialStorage?.defaultCredential(for: challenge.protectionSpace)
                    if credential != nil {
                         disposition = .useCredential
                    }
               }
          }
          return (disposition, credential)
     }

//Web service Request
let parameters = [
“username”: “TEST”,
“password”: “PASSWORD”,
]
let header: HTTPHeaders = [“Accept”: “application/json”]
LoginService.Manager.request(“https://devportal:8443/rest/login”, method: .post, parameters: parameters, encoding: JSONEncoding(options: []),headers :header).responseJSON { response in
debugPrint(response)

                   if let json = response.result.value {
                        print("JSON: \(json)")
                   }
              }



         }
    }

Thanks!! Is this the original post for your change? https://stackoverflow.com/questions/36098485/swift-how-to-make-https-request-using-server-ssl-certificate/36101084

But the post mentioned that this block of code needs to be removed for production… So, if I have to post this app to the appstore (which I have to), then I can’t apply this change to mine… ?