Swift, check if a specific website is available

How to check the availability of a specific website?

I am connected to a Wi-Fi network to access the Internet, which blocked some sites. How to check if I have access to these sites or not?

I checked the Reachability class, but I cannot check the specific site.

I am currently using Reachability.swift

+5
source share
2 answers

I do not know what is best, but I am using an HTTP request for this.

 func checkWebsite(completion: @escaping (Bool) -> Void ) { guard let url = URL(string: "yourURL.com") else { return } var request = URLRequest(url: url) request.timeoutInterval = 1.0 let task = URLSession.shared.dataTask(with: request) { data, response, error in if let error = error { print("\(error.localizedDescription)") completion(false) } if let httpResponse = response as? HTTPURLResponse { print("statusCode: \(httpResponse.statusCode)") // do your logic here // if statusCode == 200 ... completion(true) } } task.resume() } 
+1
source

The initializer you want to use is listed on this page.

You pass the hostname as a parameter:

 init?(hostname: String) // example Reachability(hostname: "www.mydomain.com") 
-2
source

Source: https://habr.com/ru/post/1268379/


All Articles