ios 在我的手机上运行 Swift 项目时,TIC TCP Conn 失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46964781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:36:06  来源:igfitidea点击:

TIC TCP Conn Failed when running Swift project on my phone

iosswift

提问by Aster

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.

我是斯威夫特的新手。我构建了一个在模拟器上运行良好的简单应用程序。我在我的设备上运行相同的设备(带有 iOS 11.0.2 的 iPhone 6s)并且它无法连接到服务器。

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 solveThanks!

添加 info.plist 的屏幕截图,我已按照其他帖子中的指示将 Allow Arbitrary Loads 设置为 True,但没有解决谢谢!

回答by Rob

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

错误 -1004 是URLError.cannotConnectToHost. 由于某种原因,它无法连接到服务器。

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 isyour computer, the localhost.

在评论中,您说 URL 是http://127.0.0.1。即localhost当前机器。如果您在物理电话上使用该 URL,它将在电话上查找 Web 服务器哈哈。它适用于模拟器,因为模拟器您的计算机,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).

您需要一个 URL,您的 iPhone 可以将其解析为运行您的 Web 服务的机器。例如,找出在您的本地网络上运行 Web 服务的计算机的 IP,确保您的 iPhone 在同一网络上的 wifi 上,然后在 LAN 上为您的计算机使用该唯一 IP 号(可能更像192.168.0.x)。

回答by Mr. Bean

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 !!

我在发送/接收套接字消息时遇到了同样的问题!我正在使用 Socket.io 和 Firebase 进行推送,错误发生在 node.js 后端。错误是猫鼬版本已弃用。每当 iOS 端出现此错误时,请让 node.js 后端团队查找红色崩溃日志。他们肯定会在前端找到导致此错误的问题。希望这可以帮助 !!

回答by iOS

In my case i'm not changes the Local host IP address.

就我而言,我不会更改本地主机 IP 地址。

Example : @"http://Localhost/Host name/index.pnp/apiname?";

initially i'm used above URL, so i'm got the same error...

最初我在 URL 上面使用,所以我遇到了同样的错误...

Later i changed in to Localhost to 190.177.0.22

后来我改成Localhost到190.177.0.22

Example : @"http://190.177.0.22/Host name/index.pnp/apiname?";

回答by Babbz77

Another corner case if you using something like ngrok and it's still not working is the phone isn't connected to a service provider network or wifi.

如果您使用诸如 ngrok 之类的东西但它仍然无法正常工作,则另一个极端情况是手机未连接到服务提供商网络或 wifi。

In my case my main phone was working the second out of service phone was not.

在我的情况下,我的主电话正在工作,第二个停止服务的电话却没有。