使用Swift中的可达性,NSNotification和网络链接调节器检测网络连接变化 [英] Detecting Network Connectivity Changes using Reachability, NSNotification and Network Link Conditioner in Swift

查看:108
本文介绍了使用Swift中的可达性,NSNotification和网络链接调节器检测网络连接变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将网络连接检测功能集成到我的应用中,但是由于我的网络更改没有被检测到/打印到控制台中,因此我似乎出现了错误。 。

I'm trying to integrate network connectivity detection into my app, however it seems that somewhere along the line I have made a mistake as my network changes are not being detected/printed out into the console.

如帖子中所述,我目前正在使用以下课程和工具:

As mentioned in the post, I'm currently using these following classes and tools for the job:


  1. 可达性 {。h,.m}

  2. NSNotificationCenter

  3. 网络链接调节器

代码

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

In the AppDelegate.Swift, I've set up the NSNotificationCenter to detect changes:

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 中,我还创建了这个函数,只要有变化就会触发:

In the same class AppDelegate, I've also created this function to be triggered whenever there is a change:

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")
    }
}

然而,当使用网络链接调节器来操纵和模拟条件的变化时,我无法看到任何这些变化反映在控制台。任何帮助都会膨胀!

However, when using the Network Link Conditioner to manipulate and simulate changes in conditions, I haven't been able to see any of those changes reflected in the console. Any help would be swell!

推荐答案

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

You must create a Reachability object before you can receive notifications from it. Also, be sure to call the startNotifier() method on the Reachability object you create. This would be an example of how to do so inside of your application delegate:

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

    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")
        }
    }
}

我建议您查看 NSNotificationCenter NSNotification 。这样你就会更熟悉下次出现这种情况时如何使用通知。

I recommend you take a look at the documentation for NSNotificationCenter and NSNotification. That way you'll be more familiar with how to work with notifications next time something like this comes up.

Swift 3

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

这篇关于使用Swift中的可达性,NSNotification和网络链接调节器检测网络连接变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆