代码之家  ›  专栏  ›  技术社区  ›  deadcoder0904

当WiFi路由器颜色在Swift中由绿色变为红色时,如何得到通知?

  •  -2
  • deadcoder0904  · 技术社区  · 7 年前

    当我的WiFi路由器颜色从 绿色

    我正在做一个应用程序,它会告诉你,你是在线还是离线从菜单栏在Swift这是开源&可在以下位置找到 https://github.com/deadcoder0904/net-alert

    我想知道是否有可能得到通知时,WiFi的颜色变化迅速。

    我不能不断ping我的服务器知道颜色改变了,因为这将浪费互联网资源。

    2 回复  |  直到 5 年前
        1
  •  4
  •   nayem    6 年前

    首先,我要从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
                }
            }
        }
    }
    
        2
  •  0
  •   daris mathew    7 年前

    我浏览了代码,你这样做的方式不会在互联网连接中断时通知你,它只是在运行时检查是否有连接。 为了达到你想要的,我们需要补充

    首先,声明可达性var

     private var reachability:Reachability!;
    

    然后将以下代码添加到 didFinishLaunchingWithOptions 中的方法 appdelegate

     self.reachability = Reachability.init()
             NotificationCenter.default.addObserver(self, selector: #selector(checkForReachability(notification:)), name: .reachabilityChanged, object: reachability)
            do{
                try reachability.startNotifier()
            }catch{
                print("could not start reachability notifier")
            }
    

    我们所做的是初始化可达性变量,并创建一个通知观察器来检测连接状态何时在通知程序的帮助下发生变化。

    最后选择方法是

    @objc func checkForReachability(notification:NSNotification)
        {
            let reachability = notification.object as! Reachability
            switch reachability.connection {
            case .wifi:
                print("Reachable via WiFi")
            case .cellular:
                print("Reachable via Cellular")
            case .none:
                print("Network not reachable")
            }
        }