Ping a Website or an IP Address (or Check if a Website is Online) using Swift 4?

simpleping swift 4
swift check if internet access
swift check no internet
check if internet connection is available ios swift
swift test internet
swift check if url is reachable
how to check internet connection in ios programmatically swift
reachability swift

I have been searching since yesterday for a simpler solution to just ping a website & check if it returns 200 in Swift.

But all I'm finding are solutions in Objective C.

In Swift, I found some answers like

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()
    }

But when I call it from some other function like

self.pingHost("https://www.google.com")

It gives weird errors like

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

How do I just simply ping in Swift 4 & check if it returns 200?


I guess the problem is easy: You enabled App Sandbox but didn't check Outgoing Connections.

What about your pingHost method - it's completely correct. So I think only problem is in App Sandbox settings.

Ping a Website or an IP Address (or Check if a Website is Online , If you're going to "ping" a website you'll want to use a HEAD request instead of a GET request. If you're developing for iOS though you need to disable App Transport withTimeout: timeoutInterval) print(configuration) SwiftPing.ping(host:​  If you're going to "ping" a website you'll want to use a HEAD request instead of a GET request. To see if a website is up you don't need the whole website, just the headers. It will save time and bandwidth: import Foundation import PlaygroundSupport PlaygroundPage.current.needsIndefiniteExecution = true if let url =


If you're going to "ping" a website you'll want to use a HEAD request instead of a GET request. To see if a website is up you don't need the whole website, just the headers. It will save time and bandwidth:

import Foundation
import PlaygroundSupport

PlaygroundPage.current.needsIndefiniteExecution = true

if let url = URL(string: "https://apple.com") {
  var request = URLRequest(url: url)
  request.httpMethod = "HEAD"

  URLSession(configuration: .default)
    .dataTask(with: request) { (_, response, error) -> Void in
      guard error == nil else {
        print("Error:", error ?? "")
        return
      }

      guard (response as? HTTPURLResponse)?
        .statusCode == 200 else {
          print("down")
          return
      }

      print("up")
    }
    .resume()
}

(Omit the playground stuff if not running in a playground.)

How to Ping External host from Swift in iOS?, Ping a Website or an IP Address (or Check if a Website is Online) using Swift 4? If you're going to "ping" a website you'll want to use a HEAD request instead of The company I got hired at is a huge software company that's been around for​  The Ping utility is an online free tool that help you to verify if a domain/server is operating and network accessible. This Ping tool uses the Internet Control Message Protocol (ICMP) Echo function as detailed in RFC 792. A small packet will be sent through the network to a given IP address (IPv4) or host name.


If you're developing for MacOS Anton's answer is correct. If you're developing for iOS though you need to disable App Transport Security(ATS) if you're pinging a non-secure URL. In order to do that you need to set the NSAllowsArbitraryLoads to true under the NSAppTransportSecurity field in the Info.plist.

More information at: https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html - NSAppTransportSecurity

How to troubleshoot network connectivity using ping and traceroute, Here we will be seeing how to check whether the external website is up and running. Let's being by Creating new project. Step 1 − Open Xcode  Check the reachability of a host with Ping from USA, Canada, Europe and other places. No need to ping manually website/server from different places. Ping hostname in automatic mode with our online service from many places around the world in one click. Ping the world and be happy! Examples: 4.2.2.2. www.apple.com. www.google.com


There is a third party library that you can use to achieve the same.

https://github.com/ankitthakur/SwiftPing

let pingInterval:TimeInterval = 3
let timeoutInterval:TimeInterval = 4
let configuration = PingConfiguration(pInterval:pingInterval, 
withTimeout:  timeoutInterval)

print(configuration)

SwiftPing.ping(host: "google.com", configuration: configuration, 
queue: DispatchQueue.main) { (ping, error) in
print("\(ping)")
print("\(error)")

}

SwiftPing.pingOnce(host: "google.com", configuration: 
configuration, 
queue: DispatchQueue.global()) { (response: PingResponse) in
print("\(response.duration)")
print("\(response.ipAddress)")
print("\(response.error)")

}

class PingResponse : NSObject {

public var identifier: UInt32

public var ipAddress: String?

public var sequenceNumber: Int64

public var duration: TimeInterval

public var error: NSError?

}

https://github.com/naptics/PlainPing

PlainPing.ping("www.google.com", withTimeout: 1.0, completionBlock: { 
(timeElapsed:Double?, error:Error?) in
if let latency = timeElapsed {
    self.pingResultLabel.text = "latency (ms): \(latency)"
}

if let error = error {
    print("error: \(error.localizedDescription)")
}
})

Online Ping Utility Tool for IPv4 Address, Ping a Website or an IP Address (or Check if a Website is Online) using Swift 4? swift check if server is reachable simpleping swift 4 plain ping  Ping (Packet internet groper) is a very useful network tool that is mostly used to check if a remote host is online and "alive". This ping tool can test the reachability of a host and show details about packets transmitted, received and lost during the ping request. Quickly ping a host or IPv4 address online for free.


Change dns mac terminal, Learn How to Troubleshoot Network Connectivity with Tracert & Ping Test. Traceroute is especially helpful for diagnosing where network slowdowns and test the connection between your computer and a remote host, a web-based tool It also displays the domain and IP address at each hop, as well as the time spent. The lower the number, the faster the connection between your computer and the other computer or website. When you ping a web address in the terminal, the second line shows the IP address of the website you are pinging. You can use that to ping a website instead of the IP address. Stop the ping process.


How to Run Ping (Network Diagnostics) on Your iPhone, This Ping tool uses the Internet Control Message Protocol (ICMP) Echo A small packet will be sent through the network to a given IP address (IPv4) or host name. Ping can also tell the user the number of hops between two targets and the You can easily add the ping widget on your website by copying the following  Port check – Tests if TCP port is opened on specified IP. Reverse lookup – Gets hostname by IP address. Proxy checker – Detects a proxy server. Bandwidth meter – Detects your download speed from our server. Network calculator – Calculates subnet range by network mask. Network mask calculator – Calculates network mask by subnet range.


Network Latency: 2019 Guide and Best Latency Testing Tools , 81) — that's how your browser is actually able to find the website you want. 4. First, ping google. Click Continue until you get back to the Printers page. scutil Jan 12, 2018 · To check if your Mac computer is infected with MaMi malware, go to OS X 12 (Sierra) and later To change the IP address and DNS server of your​  The IP address of the network adapter that you want to check. The IP address of your default gateway. Use Ping.exe to Check Your Hardware To do this: At the command prompt, type ping loopback /localhost 127.0.0.1, and then press ENTER. The result should be similar to the following: