如何检测iPhone是否已连接到互联网?

时间:2015-10-02 10:20:33

标签: ios iphone swift wifi

一旦设备连接到wifi,我需要在我的应用中制作一些东西。这不是关于“它是否连接到wifi?”而是“告诉我什么时候设备连接到wifi,而它之前是脱机的?”

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

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

2 个答案:

答案 0 :(得分: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)
        }
    }
}

答案 1 :(得分:1)

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()