【问题标题】:How to detect if iPhone JUST connected to internet?如何检测 iPhone 是否刚刚连接到互联网?
【发布时间】:2015-10-02 10:20:33
【问题描述】:

一旦设备连接到 wifi,我需要在我的应用中做一些事情。这不是关于“它是否连接到 wifi?”,而是“告诉我设备何时连接到 wifi,而之前它处于离线状态?”

换句话说,当 iPhone 以任何方式连接到互联网时,我需要响应事件。

是否可以使用AFNetworking 或自定义 API?

【问题讨论】:

    标签: ios iphone swift wifi


    【解决方案1】:

    您可以使用Reachability

    在示例代码中你会发现:

    import UIKit
    
    let useClosures = false
    
    class ViewController: UIViewController {
    
        @IBOutlet weak var networkStatus: UILabel!
    
        let reachability = Reachability.reachabilityForInternetConnection()
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            if (useClosures) {
                reachability?.whenReachable = { reachability in
                    self.updateLabelColourWhenReachable(reachability)
                }
                reachability?.whenUnreachable = { reachability in
                    self.updateLabelColourWhenNotReachable(reachability)
                }
            } else {
                NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name: ReachabilityChangedNotification, object: reachability)
            }
    
            reachability?.startNotifier()
    
            // Initial reachability check
            if let reachability = reachability {
                if reachability.isReachable() {
                    updateLabelColourWhenReachable(reachability)
                } else {
                    updateLabelColourWhenNotReachable(reachability)
                }
            }
        }
    
        deinit {
    
            reachability?.stopNotifier()
    
            if (!useClosures) {
                NSNotificationCenter.defaultCenter().removeObserver(self, name: ReachabilityChangedNotification, object: nil)
            }
        }
    
        func updateLabelColourWhenReachable(reachability: Reachability) {
            if reachability.isReachableViaWiFi() {
                self.networkStatus.textColor = UIColor.greenColor()
            } else {
                self.networkStatus.textColor = UIColor.blueColor()
            }
    
            self.networkStatus.text = reachability.currentReachabilityString
        }
    
        func updateLabelColourWhenNotReachable(reachability: Reachability) {
            self.networkStatus.textColor = UIColor.redColor()
    
            self.networkStatus.text = reachability.currentReachabilityString
        }
    
    
        func reachabilityChanged(note: NSNotification) {
            let reachability = note.object as! Reachability
    
            if reachability.isReachable() {
                updateLabelColourWhenReachable(reachability)
            } else {
                updateLabelColourWhenNotReachable(reachability)
            }
        }
    }
    

    【讨论】:

    • 它会通知我...如何?
    • 但请务必取消订阅可达性通知,因为您可能会被多次调用“我有互联网连接”。
    • 是的,只需从那里测试一个演示。
    • 我如何获得Reachibility 类?
    【解决方案2】:

    AFNetworking 的简单用法:

    1. 添加观察者:

      NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("networkDidChangeStatus"), name: AFNetworkingReachabilityDidChangeNotification, object: nil)
      
    2. 实现自定义Selector:

      func networkDidChangeStatus() {
      
          if AFNetworkReachabilityManager.sharedManager().reachable {
              print("connected")
          } else {
              print("disconnected")
          }
      }
      
    3. 开始监控:

      AFNetworkReachabilityManager.sharedManager().startMonitoring()
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-27
      • 2022-01-14
      • 1970-01-01
      • 2011-09-02
      • 2023-03-30
      • 1970-01-01
      相关资源
      最近更新 更多