Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TIC TCP Conn Failed when running Swift project on my phone

Tags:

ios

swift

I am new to Swift. I built a simple application which works fine on the simulator. I am running the same on my device (iPhone 6s with iOS 11.0.2) and it's failing to connect to server.

getting these errors:

2017-10-26 18:16:02.489134-0400 myproj[1451:206438] TIC TCP Conn Failed [1:0x1c0176800]: 1:61 Err(61)
2017-10-26 18:16:02.489771-0400 myproj[1451:206438] Task <0C30ADDC-4A0E-4815-A701-2EF0A7CF5F04>.<1> HTTP load failed (error code: -1004 [1:61])
2017-10-26 18:16:02.490293-0400 myproj[1451:206440] Task <0C30ADDC-4A0E-4815-A701-2EF0A7CF5F04>.<1> finished with error - code: -1004

Please help me understand this error.

EDIT:

Here is the code making that call to the server:

func postRequest(postData: NSDictionary, postHeaders: NSDictionary, endPoint: String,
                 onComplete: @escaping ((NSDictionary)->Void), callbackParams: NSDictionary = NSMutableDictionary()) {
    let url:URL = baseUrl.appendingPathComponent(endPoint)
    let session = URLSession.shared
    let request = NSMutableURLRequest(url: url)
    request.httpMethod = "POST"
    request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
    var paramString = ""
    for (key, value) in postData{
        paramString = paramString + (key as! String) + "=" + (value as! String) + "&"
    }

    request.allHTTPHeaderFields = postHeaders as? [String : String]
    request.httpBody = paramString.data(using: String.Encoding.utf8)

    let task = session.dataTask(with: request as URLRequest, completionHandler: {
        (data, response, error) in
        guard let _:Data = data, let _:URLResponse = response  , error == nil else {
            return}
        let json: Any?
        do {
            json = try JSONSerialization.jsonObject(with: data!, options: [])
        }
        catch {
            return
        }
        var serverResponse = json as? NSDictionary
        DispatchQueue.main.async{
            for (key, value) in serverResponse!{
                callbackParams.setValue(value, forKey: key as! String)
            }
            onComplete(callbackParams)
        }
    })
    task.resume()
}

EDIT:

Adding screen shot of info.plist, I had set Allow Arbitrary Loads to True as indicated in other posts, but didn't solve Thanks!

like image 754
Aster Avatar asked Oct 26 '17 22:10

Aster


2 Answers

Error -1004 is URLError.cannotConnectToHost. It cannot connect to the server for some reason.

In comments, you say the URL is http://127.0.0.1. That is localhost, the current machine. If you use that URL on a physical phone, it's going to look for the web server on the phone. Lol. It works on the simulator, because the simulator is your computer, the localhost.

You need a URL that your iPhone can resolve to the machine running your web service. For example, find out what the IP for the computer running the web service on your local network, make sure your iPhone is on wifi on the same network, and then use that unique IP number for your computer on the LAN (likely something more like 192.168.0.x).

like image 67
Rob Avatar answered Sep 28 '22 00:09

Rob


I was getting the same problem when i was sending/ receiving socket messages! I am using Socket.io and Firebase for push and the error was at the node.js backend. The error was that the mongoose version was deprecated. Whenever this error arises at the iOS End, ask the node.js backend team to look for Red color crash logs. They will surely find the problem causing this error on frontend. Hope this helps !!

like image 41
Mr. Bean Avatar answered Sep 27 '22 23:09

Mr. Bean