1

I am getting this

"load failed with error Error Domain=NSURLErrorDomain Code=-999 "cancelled""

when trying to call the api in my custom framework. but when I run the same code in another project, it works fine and give the proper response. below is my code. Is there anything I have to do in custom framework which does not require in normal project.

func invokePostWebServiceCallLogin(request : String,param : NSDictionary,completion : @escaping (_ webResponse : WebserviceResponseClass) ->Void) -> Void {

        let headers = [
            "Accept": "application/json",
            "Content-Type": "application/x-www-form-urlencoded"
        ]
        let passingParameter : [ String : AnyObject] = param as! [String : AnyObject]

        UIApplication.shared.isNetworkActivityIndicatorVisible = true

        WebServiceHelperClass.Manager.request(URL(string: request)!, method: .post, parameters: passingParameter,encoding: URLEncoding.default, headers: headers).validate().responseJSON {
            response in
            UIApplication.shared.isNetworkActivityIndicatorVisible = false

            switch response.result {
            case .success:
                let webResult =  WebserviceResponseClass()
                webResult.isSuccess = true
                webResult.responseData = response.data as NSData?
                webResult.error = nil
                completion(webResult)
                break
            case .failure(let error):
                let webResult =  WebserviceResponseClass()
                webResult.isSuccess = false
                webResult.responseData = nil
                webResult.error = error as NSError
                completion(webResult)
            }
        }
    }

    private static var Manager : Alamofire.SessionManager = {
         let    serverTrustPolicies: [String: ServerTrustPolicy] = [
                "dev.xxxxx.com": .pinCertificates(
                    certificates: ServerTrustPolicy.certificates(),
                    validateCertificateChain: true,
                    validateHost: true
            ),
                "xx.xx.xxx.xx": .disableEvaluation
            ]

        let configuration = URLSessionConfiguration.default
        configuration.timeoutIntervalForRequest = 45
        configuration.timeoutIntervalForResource = 45

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

Task <91C7555E-F6E6-45EB-9762-EE61915719DE>.<1> load failed with error Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo={NSErrorFailingURLStringKey=https://dev.xxxxxxxx.com:xxxx/api/VirtualAPI/Login, NSErrorFailingURLKey=https://dev.xxxxxxxx.com:xxxx/api/VirtualAPI/Login, _NSURLErrorRelatedURLSessionTaskErrorKey=( "LocalDataTask <91C7555E-F6E6-45EB-9762-EE61915719DE>.<1>" ), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <91C7555E-F6E6-45EB-9762-EE61915719DE>.<1>, NSLocalizedDescription=cancelled} [-999]

anirudh
  • 49
  • 1
  • 5

1 Answers1

6

Looks like the API call is using certificate pinning security policy. When the certificate pinning fails the data task will return with NSLocalizedDescription=cancelled. Check the ServerTrustPolicy.certificates() and see that it's returning a valid certificate data - usually it automatically load any certificates that are in the SAME bundle. If not make sure to load it manually

Tziki
  • 311
  • 1
  • 8
  • 1
    Hi, thanks it worked for me. I was using certificates which were added into the framework. I changed the bundle name and it worked. – anirudh Jul 26 '19 at 04:11
  • 2
    glad to hear :), you can go ahead and accept the answer. – Tziki Jul 27 '19 at 11:05