小编典典

在Swift中使用可达性,NSNotification和网络链接调节器检测网络连接变化

swift

在iOS 12中,您只需使用 NWPathMonitor
,它是一行代码(示例)。

出于历史目的:


我正在尝试将 网络连接 检测集成到我的应用程序中,但是由于没有检测到/将我的网络更改未打印/打印到控制台中,因此我似乎犯了一个错误。

正如文章中提到的,我目前正在使用以下这些类和工具来完成工作:

  1. 可达性 {.h, .m}
  2. NSNotificationCenter
  3. 网络链接调节器

AppDelegate.Swift中 ,我设置了NSNotificationCenter来检测更改:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// ... 
// A: Checks if the device is connected to the internet

    var defaultCenter: Void = NSNotificationCenter().addObserver(self, selector:"checkForReachability", name: kReachabilityChangedNotification, object: nil)

}

在同一个类中AppDelegate,我还创建了此函数,以便在发生任何更改时触发该函数:

func checkForReachability () {

    var networkReachability = Reachability.reachabilityForInternetConnection()
    networkReachability.startNotifier()

    var remoteHostStatus = networkReachability.currentReachabilityStatus()
    if (remoteHostStatus.value == NotReachable.value) {
        println("Not Reachable")
    } else if (remoteHostStatus.value == ReachableViaWiFi.value) {
        println("Reachable via Wifi")
    } else {
        println("Reachable")
    }
}

但是,当使用Network Link Conditioner操纵和模拟条件变化时,我无法在控制台中看到任何变化。任何帮助都会膨胀!


阅读 276

收藏
2020-07-07

共1个答案

小编典典

您必须先创建一个“可达性”对象, 然后
才能接收来自它的通知。另外,请确保startNotifier()在创建的Reachability对象上调用该方法。这将是一个如何在您的应用程序委托内部执行此操作的示例:

class AppDelegate: UIResponder, UIApplicationDelegate
{
    private var reachability:Reachability!;

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool
    {
        NSNotificationCenter.defaultCenter().addObserver(self, selector:"checkForReachability:", name: kReachabilityChangedNotification, object: nil);

        self.reachability = Reachability.reachabilityForInternetConnection();
        self.reachability.startNotifier();
    }

    @objc func checkForReachability(notification:NSNotification)
    {
        // Remove the next two lines of code. You cannot instantiate the object
        // you want to receive notifications from inside of the notification
        // handler that is meant for the notifications it emits.

        //var networkReachability = Reachability.reachabilityForInternetConnection()
        //networkReachability.startNotifier()

        let networkReachability = notification.object as Reachability;
        var remoteHostStatus = networkReachability.currentReachabilityStatus()

        if (remoteHostStatus.value == NotReachable.value)
        {
            println("Not Reachable")
        }
        else if (remoteHostStatus.value == ReachableViaWiFi.value)
        {
            println("Reachable via Wifi")
        }
        else
        {
            println("Reachable")
        }
    }
}

我建议您看一下NSNotificationCenterNSNotification的文档。这样,您将在下次出现类似情况时更加熟悉如何使用通知。

迅捷3

NotificationCenter.default.addObserver(self, selector:Selector(("checkForReachability:")), name: NSNotification.Name.reachabilityChanged, object: nil)
let reachability: Reachability = Reachability.forInternetConnection()
reachability.startNotifier()
2020-07-07