I'm very much new to ios && using cocoapods.
I scanned over SO to find easiest way to detect network status and lots of answers directed me to Reachability git by AshleyMills.
I'm writing my webView app in swiftui & I want to pop up an alert/notifier when user's internet connection is lost. (So they don't sit idle while my webview tries to load)
It would be best if listner to network changes keeps running in the background while the app is on.
Most of the answers in SO seem to be Swift-based (appdelegate, ViewDidload, etc), which I don't know how to use because I started off with SwiftUI
Thanks in advance.
Edit(With attempts for Workaround that Lukas provided)
I tried the below. It compiles and runs BUT the alert wouldn't show up. Nor does it respond to connection change.
I have number of subViews in my ContentView. So I called timer && reachability on app level.
@State var currentDate = Date()
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
let reachability = try! Reachability()
@State private var showAlert: Bool = false
It seems to be not working:
WindowGroup {
ContentView()
.onAppear() {
if !isConnected() {
self.showAlert = true
}
}
.onReceive(timer) { _ in
if !isConnected() {
self.showAlert = true
}else{
self.showAlert = false
}
}
.alert(isPresented: $showAlert) {
Alert(title: Text("Error"), message: Text("Your internet connection is too slow."), dismissButton: .default(Text("ok")))
}
}