gpt4 book ai didi

ios - 连接总是给我假的?

转载 作者:行者123 更新时间:2023-11-28 13:45:05 25 4
gpt4 key购买 nike

我正在使用 Reachability.swift 来测试互联网连接是(关闭/打开)和服务器是(活/死)服务器检查工作正常但互联网连接总是给我错误?

   @IBAction func TestNetwork(_ sender: Any) {
var internetConnection = "❌"
var serverStatus = "❌"
var message = " \(internetConnection) internet connection \n \(serverStatus) MHS server\n "
let reachability = Reachability(hostname:"google.com")
if (reachability?.connection != .none ) {
serverStatus = "✅"
message = " \(internetConnection) internet connection \n \(serverStatus) goole server\n "

} else {
serverStatus = "❌"
message = " \(internetConnection) internet connection \n \(serverStatus) google server\n "
}
if (reachability?.connection == .wifi && reachability?.connection == .cellular) {
internetConnection = "✅"
message = " \(internetConnection) internet connection \n \(serverStatus) google server\n "
} else {
internetConnection = "❌"
message = " \(internetConnection) internet connection \n \(serverStatus) google server\n "
}

let alertController = UIAlertController(title: "Alert", message: message, preferredStyle: .alert)
let defaultAction = UIAlertAction(
title: "OK", style: .default, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
}

最佳答案

连接是一个有 3 种情况的枚举:

enum Connection {
case none, wifi, cellular
}

由于您的 reachability?.connection 只能是其中之一,因此您需要将检查从 && 更改为 ||

改变:

if (reachability?.connection == .wifi && reachability?.connection == .cellular) {
internetConnection = "✅"
message = " \(internetConnection) internet connection \n \(serverStatus) google server\n "
}

收件人:

if (reachability?.connection == .wifi || reachability?.connection == .cellular) {
internetConnection = "✅"
message = " \(internetConnection) internet connection \n \(serverStatus) google server\n "
}

关于ios - 连接总是给我假的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55498513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com