NWPath.status 在设备上显示不满意,在模拟器上显示满意

问题描述 投票:0回答:1

我在尝试验证物理设备上的 WatchOS 对互联网的访问时遇到问题。在物理设备上运行以下代码始终显示“未连接”。然而,在模拟器上运行显示“已连接”。

import Foundation
import Network

class NetworkMonitor: ObservableObject {
    
    @Published var isConnected: Bool = false
    @Published var currentNetworkType: NWInterface.InterfaceType = .other
    
    private let monitor = NWPathMonitor()
   
    init() {
        monitor.pathUpdateHandler = { [weak self] path in
            DispatchQueue.main.async {
                self?.networkStatusDidChange(path)
            }
        }
        monitor.start(queue: DispatchQueue.global(qos: .background))
    }
  
    private func networkStatusDidChange(_ path: NWPath) {
        isConnected = path.status == .satisfied
        currentNetworkType = path.availableInterfaces.first?.type ?? .other
    }
    
    deinit {
        monitor.cancel()
    }

}

import Foundation
import Network

class NetworkMonitor: ObservableObject {
    
    @Published var isConnected: Bool = false
    @Published var currentNetworkType: NWInterface.InterfaceType = .other
    
    private let monitor = NWPathMonitor()
   
    init() {
        monitor.pathUpdateHandler = { [weak self] path in
            DispatchQueue.main.async {
                self?.networkStatusDidChange(path)
            }
        }
        monitor.start(queue: DispatchQueue.global(qos: .background))
    }
  
    private func networkStatusDidChange(_ path: NWPath) {
        isConnected = path.status == .satisfied
        currentNetworkType = path.availableInterfaces.first?.type ?? .other
    }
    
    deinit {
        monitor.cancel()
    }

}
apple-watch watchos
1个回答
0
投票

我也看到了同样的行为。似乎不适用于 Apple Watch。

© www.soinside.com 2019 - 2024. All rights reserved.