Swift, Check if particular website reachable

Viewed 4383

How to check reachability of particular website?

I am connected to wifi network for internet access, which have blocked some sites. How to check if I have access to those sites or not?

I have checked with Reachability class, but I can not check for particular website.

Currently I am using Reachability.swift

3 Answers
func pageExists(at url: URL) async -> Bool {
    var headRequest = URLRequest(url: url)
    headRequest.httpMethod = "HEAD"
    headRequest.timeoutInterval = 3
    let headRequestResult = try? await URLSession.shared.data(for: headRequest)
    
    guard let httpURLResponse = headRequestResult?.1 as? HTTPURLResponse
    else { return false }
    
    return (200...299).contains(httpURLResponse.statusCode)
}
Related