从昨天开始,我一直在寻找一个更简单的解决方案,以便仅对网站进行ping并检查它在Swift中是否返回200。
但是我发现的只是目标C中的解决方案。
在Swift中,我找到了一些答案,例如
func pingHost(_ fullURL: String) { let url = URL(string: fullURL) let task = URLSession.shared.dataTask(with: url!) { _, response, _ in if let httpResponse = response as? HTTPURLResponse { print(httpResponse.statusCode) } } task.resume() }
但是当我从其他函数调用它时
self.pingHost("https://www.google.com")
它给出了奇怪的错误,例如
2018-09-26 12:46:34.076938+0530 Net Alert[1608:52682] dnssd_clientstub ConnectToServer: connect()-> No of tries: 1 2018-09-26 12:46:35.082274+0530 Net Alert[1608:52682] dnssd_clientstub ConnectToServer: connect()-> No of tries: 2 2018-09-26 12:46:36.083497+0530 Net Alert[1608:52682] dnssd_clientstub ConnectToServer: connect()-> No of tries: 3 2018-09-26 12:46:37.083964+0530 Net Alert[1608:52682] dnssd_clientstub ConnectToServer: connect() failed path:/var/run/mDNSResponder Socket:5 Err:-1 Errno:1 Operation not permitted 2018-09-26 12:46:37.084497+0530 Net Alert[1608:52682] [] nw_resolver_create_dns_service_locked [C1] DNSServiceCreateDelegateConnection failed: ServiceNotRunning(-65563) 2018-09-26 12:46:37.087264+0530 Net Alert[1608:52682] TIC TCP Conn Failed [1:0x600003706e80]: 10:-72000 Err(-65563) 2018-09-26 12:46:37.088841+0530 Net Alert[1608:52673] Task <2B08658D-5DFA-48E9-A306-A47ED130DD1F>.<1> HTTP load failed (error code: -1003 [10:-72000]) 2018-09-26 12:46:37.088990+0530 Net Alert[1608:52673] Task <2B08658D-5DFA-48E9-A306-A47ED130DD1F>.<1> finished with error - code: -1003
我如何简单地在Swift 4中ping并检查它是否返回200?
我想这个问题很容易:您启用了 应用沙箱 , 但未选中 传出连接 。
那你的pingHost方法呢-完全正确。因此,我认为唯一的问题是应用程序沙箱设置。
pingHost