0
votes

I have integrated Alamofire Library for API calling, and getting following error :

Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo={_kCFStreamErrorCodeKey=57, NSUnderlyingError=0x600000c68990 {Error Domain=kCFErrorDomainCFNetwork Code=-1005 "(null)" UserInfo={_kCFStreamErrorCodeKey=57, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask .<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=( "LocalDataTask .<1>" ), NSLocalizedDescription=The network connection was lost., NSErrorFailingURLStringKey=OUR_Server_URL, NSErrorFailingURLKey=OUR_Server_URL, _kCFStreamErrorDomainKey=1}

I have added following property into .plist file too :

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

I am using Alamofire using following code :-

Alamofire code :

func postFormDataWebService(methodType : HTTPMethod, contentType : String, url : String,
                                  parameters : NSMutableDictionary?,
                                  success: @escaping(_ response : NSDictionary, _ status : String) -> Void,
                                  failure: @escaping(_ error : Error, _ status : Int) -> Void){

    var headers : HTTPHeaders = [ "Content-Type": contentType, "accept": "application/json"]

    Alamofire.request(url, method: methodType, parameters:parameters as? [String : Any] , encoding: JSONEncoding.default, headers: headers)
        .responseJSON {
        (response:DataResponse) in
        var responseStatusCode: Int = 0
        switch response.result {

        case .success:
            responseStatusCode = (response.response?.statusCode)!
            let data = response.result.value as! NSDictionary
            print(data)
            success(data, data["status"] as? String ?? "")

            break
        case .failure(let error):
            responseStatusCode = response.response?.statusCode ?? 0
            print("statusCode :: ", response.response?.statusCode  ?? 0)
            print("Error :: ",error.localizedDescription)                
            failure(error,responseStatusCode)
        }
    }
}

API Calling function :

ApiManager.shared.postFormDataWebService(methodType: :POST, contentType: "application/json", url: LoginUrl, parameters: parameters, 
success: { (response, status) in
    print("response = \(response)")
    print("status = \(status)")
}) { (error, status) in
    print("Login Error == \(error)\nStatus == \(status)")
}

Any help would be appreciated.

1
Well I guess someone has to ask the obvious question, did you check your network connection on your computer/phone when this happened?Joakim Danielson
seems your connection is passing through a proxy server, use wifi also completely removing LAN cable from system, if it not worked you can go with restart system.Govind Wadhwa

1 Answers

1
votes

You just need to enable this option from your capabilities.

enter image description here

Hope, this is what you're looking for. Any concern get back to me. :)