首先,我要从iOS的角度来回答这个问题。但是你的GitHub演示是针对macOS的。我觉得基本的都一样。
我将用面向协议的方法来解决这个问题。
更新:
Connectivity
Solving the Captive Portal Problem on iOS
这个实现能够处理实际的互联网可用/不可用状态。
不想再读下去吗?
Here
绿色
&
红色
不同连接状态的颜色。
定义协议:
protocol ConnectivityNotifiable {
var connectivity: Connectivity { get }
func startNotifyingConnectivityChangeStatus()
func stopNotifyingConnectivityChangeStatus()
func connectivityChanged(toStatus: ConnectivityStatus)
}
// Provide some default implementation through protocol extension
extension ConnectivityNotifiable {
func startNotifyingConnectivityChangeStatus() {
connectivity.isPollingEnabled = true
connectivity.startNotifier()
connectivity.whenConnected = { connectivity in
self.connectivityChanged(toStatus: connectivity.status)
}
connectivity.whenDisconnected = { connectivity in
self.connectivityChanged(toStatus: connectivity.status)
}
}
func stopNotifyingConnectivityChangeStatus() {
connectivity.stopNotifier()
}
}
遵守协议:
ConnectivityNotifiable
协议将向任何感兴趣的对象添加功能,以便在连接状态更改时得到通知。类似于监视。
class ViewController: UIViewController, ConnectivityNotifiable {
// ConnectivityNotifiable protocol requirement
let connectivity = Connectivity()
override func viewDidLoad() {
super.viewDidLoad()
// Invoke the default implementation of the ConnectivityNotifiable protocol
// requirement to be able to be notified
startNotifyingConnectivityChangeStatus()
// Reminder:
// Don't forget to invoke stopNotifyingConnectivityChangeStatus() when
// you are done or when the lifecycle of your controller ends
}
// ConnectivityNotifiable protocol requirement
func connectivityChanged(toStatus: ConnectivityStatus) {
// Everytime any change happens in the network connectivity
// this method will be invoked with appropriate connection status
switch toStatus {
case .connected,
.connectedViaWiFi,
.connectedViaCellular:
// Connected/Internet available. Update any UI component
case .notConnected,
.connectedViaWiFiWithoutInternet,
.connectedViaCellularWithoutInternet:
// Disconnected/Internet not available. Update any UI component
}
}
}
古老的答案
注:
如果您使用的是最新的
release
的
Reachability
,您将不需要
NotificationCenter
不想知道如何做到这一点?
Here
,
橙色
不同连接状态的颜色。
定义协议:
protocol Reachable {
var reachability: Reachability { get }
func startMonitoringReachabilityChangeStatus()
func reachabilityChanged(to: Reachability.Connection)
}
extension Reachable {
func startMonitoringReachabilityChangeStatus() {
do {
try reachability.startNotifier()
} catch {
print(error.localizedDescription)
}
reachability.whenReachable = { reachability in
self.reachabilityChanged(to: reachability.connection)
}
reachability.whenUnreachable = { reachability in
self.reachabilityChanged(to: reachability.connection)
}
}
}
遵守协议:
符合
Reachable
协议将功能添加到任何感兴趣的对象,以便在可达性状态更改时得到通知。类似于监视。
class ViewController: UIViewController, Reachable {
// Reachable protocol requirement
let reachability: Reachability = Reachability()!
override func viewDidLoad() {
super.viewDidLoad()
// initial reachability checkup
reachabilityChanged(to: reachability.connection)
// Invoke the default implementation of the Reachable protocol requirement
// to be able to be notified
startMonitoringReachabilityChangeStatus()
}
// Reachable protocol requirement
func reachabilityChanged(to: Reachability.Connection) {
// Everytime any change happens in the network connectivity
// this method will be invoked with appropriate connection status
switch to {
case .wifi:
DispatchQueue.main.async {
// Update any UI component
}
case .cellular:
DispatchQueue.main.async {
// Update any UI component
}
case .none:
DispatchQueue.main.async {
// Update any UI component
}
}
}
}