ios - NWPathMonitor 在 Swift 中调用不满意

标签 ios swift networking detection

我正在使用 NWPathMonitor检测互联网连接何时开启和关闭。该方法在两种状态都发生时被调用,但是当互联网连接打开时,被调用状态仍然是.unsatisfied而不是 .satisfied .这是我正在使用的类(class):

import Network

protocol NetworkCheckDelegate {
    func statusDidChange(status: NWPath.Status)
}

class NetworkCheck {

    private var monitor = NWPathMonitor()

    private static let _sharedInstance = NetworkCheck()

    var networkCheckDelegate: NetworkCheckDelegate?

    class func sharedInstance() -> NetworkCheck {
        return _sharedInstance
    }

    // Create only one instance of NetworkCheck
    private init() {
        monitor.pathUpdateHandler = { path in
            DispatchQueue.main.async(execute: {
                self.networkCheckDelegate?.statusDidChange(status: path.status)
            })
        }
        monitor.start(queue: DispatchQueue.global(qos: .background))
    }

    func removeMonitoring() {
        monitor.cancel()
    }
}

我的问题是:为什么互联网连接被称为 .unsatisfied在这两种情况下

最佳答案

模拟器无法准确地将网络更改传输到应用程序。
以我的经验,它将是您在启动时运行 Xcode 的设备的网络状态,并且仅当您从有连接(满意)变为没有(不满意)时才更新自身,但反之则不会。
由于此限制,您需要在真实设备上测试网络。

关于ios - NWPathMonitor 在 Swift 中调用不满意,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57223756/

相关文章:

iOS 可拖动 subview

ios - 按日期对 Firebase 数据排序

networking - MVC 到 MVVM 与网络 : Architectural appraoch/best practices

linux - 如何使 iproute2 多个上行链路与伪装一起工作

ios - 从 iOS 锁定屏幕在应用程序中自定义远程事件处理

iphone - 我怎样才能在 iPhone 应用程序中捕获所有异常

swift - SpriteKit : How to create infinite game background like 'Binary Rush' or 'Stay in the line' ?

swift 包管理器: static build failing after software updates

android - Android 的仅扫描模式如何工作?

c# - 在 Unity 中使游戏对象可触摸 - 增强现实