为什么网络库在 Swift Ios 中无法检测到何时重新连接互联网
Why does Network library fails to detect when internet is connected back in Swift Ios
我有以下代码来检测我的互联网连接状态
override func viewDidLoad() {
super.viewDidLoad()
internetStatus()
// Do any additional setup after loading the view.
}
func internetStatus(){
let nwPathMonitor = NWPathMonitor()
nwPathMonitor.start(queue: DispatchQueue(label: "network_monitor"))
nwPathMonitor.pathUpdateHandler = { path in
if (path.status == .satisfied){
if (path.usesInterfaceType(.wifi)){
print("wifi")
}
if (path.usesInterfaceType(.cellular)){
print("cellular")
}
}
if (path.status == .unsatisfied)
{
print("noConnection")
}
if (path.status == .requiresConnection){
}
}
}
这工作正常,但在 iOs 模拟器上我无法检测到重新连接。当我断开连接时打印“noConnection”很好,但是当我连接时再次打印“noConnection”。这是某种错误还是我的代码有问题
所以这在真实设备上工作正常但在模拟器上它不起作用。我正在使用 macOS Big Sur 和 XCode 12.5
我有以下代码来检测我的互联网连接状态
override func viewDidLoad() {
super.viewDidLoad()
internetStatus()
// Do any additional setup after loading the view.
}
func internetStatus(){
let nwPathMonitor = NWPathMonitor()
nwPathMonitor.start(queue: DispatchQueue(label: "network_monitor"))
nwPathMonitor.pathUpdateHandler = { path in
if (path.status == .satisfied){
if (path.usesInterfaceType(.wifi)){
print("wifi")
}
if (path.usesInterfaceType(.cellular)){
print("cellular")
}
}
if (path.status == .unsatisfied)
{
print("noConnection")
}
if (path.status == .requiresConnection){
}
}
}
这工作正常,但在 iOs 模拟器上我无法检测到重新连接。当我断开连接时打印“noConnection”很好,但是当我连接时再次打印“noConnection”。这是某种错误还是我的代码有问题
所以这在真实设备上工作正常但在模拟器上它不起作用。我正在使用 macOS Big Sur 和 XCode 12.5