I state that I am new to Swift and iOS in general.
I'd like to understand (in viewDidLoad) how I can check if the iPhone is connected to the internet, instantly, at that moment!
The new NWPathMonitor() class seems to be useful only for managing connection changes, but not for instantaneously checking the connection.
My ViewController
import UIKit
import WebKit
import Network
class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {
// Status bar black characters
override var preferredStatusBarStyle: UIStatusBarStyle { return .darkContent }
// La nostra webview
var webView: WKWebView!
// NETWORK MONITORING
let monitor = NWPathMonitor()
let queue = DispatchQueue(label: "InternetConnectionMonitor")
var internetConnected = false
// 1 - Eseguita durante il caricamento della view
override func loadView() {
super.loadView()
// WEB VIEW
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
// NETWORK MONITORING
monitor.pathUpdateHandler = { path in
if path.status == .satisfied {
print("dedalos-print INTERNET OK 1")
if (!self.internetConnected) {
self.internetConnected = true
self.loadWebView()
}
}
else {
print("dedalos-print NO INTERNET 1")
self.internetConnected = false
self.loadHTMLerror()
}
}
monitor.start(queue: queue)
}
// 2 - Eseguita dopo il caricamento della view
override func viewDidLoad() {
super.viewDidLoad()
if (self.internetConnected) {
print("dedalos-print INTERNET OK 2")
self.loadWebView()
}
else {
print("dedalos-print NO INTERNET 2")
}
}
// 3 - Eseguita poco prima di mostrarsi
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
// 4 - Eseguita dopo essersi mostrata
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
}
}
Thanks in advance!