ios - Swift,检查特定网站是否可访问

标签 ios swift reachability

如何检查特定网站的可达性?

我已连接到 wifi 网络以访问互联网,但已阻止某些站点。如何检查我是否可以访问这些网站?

我已经检查了 Reachability 类,但我无法检查特定网站。

目前我正在使用 Reachability.swift

最佳答案

我不知道什么是最佳实践,但我使用 HTTP request 来这样做。

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

关于ios - Swift,检查特定网站是否可访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44276370/

相关文章:

ios - 可达性等级无法正常用于 VPN 连接

objective-c - iOS voip 应用程序休眠和可达性已更改,我可以收到通知吗?

iphone - 检测 iOS5 上何时禁用蓝牙

swift - 创建 "Next"按钮,如何为另一个 View 设置目标?

ios - 解析 - 设置设备 token

swift - 在方法对变量进行更改后更新 UI 上的值 (Swift 3)

ios - reachabilityWithHostName 超时

ios - iPad 中 10000 条记录的 SQLite 性能

ios - 将使用 xamarin (mono touch) 构建的更新上传到使用 xcode 构建的现有应用程序

iphone - 等到 CCMoveTo 完成?