Hello,
I am encountering an issue when attempting to verify access to the internet from WatchOS on a physical device. Running the code below on a physical device always shows "Not Connected." However, running on the simulator shows "Connected."
Three years ago, a similar post was made. https://developer.apple.com/forums/thread/131678
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 SwiftUI
struct ContentView: View {
@ObservedObject var networkMonitor = NetworkMonitor()
var body: some View {
VStack {
if networkMonitor.isConnected {
Text("Connected")
} else {
Text("Not Connected")
}
}
}
}
code-block