我的可达性通知程序只能被调用一次 [英] My Reachability Notifier is only able to be called once

查看:86
本文介绍了我的可达性通知程序只能被调用一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我的AppDelegate中包含以下内容.
当我关闭WIFI时,它会通知我,但在首次运行后将不响应.
过去我一直在进行此操作.
我正在使用Xcode 8和此版本的swift和xCode的可访问性.

So, I have the following in my AppDelegate.
It will notify my when I turn my WIFI off but will not react after that initial run.
I have had this working in the past.
I'm on swift 3 with Xcode 8 and the reachability that is for this version of the swift and xCode.

我希望对此有所解决.

谢谢.

I'm hoping to get a solution to this.

Thanks.

    var reachability: Reachability?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        self.reachability = Reachability()

         NotificationCenter.default.addObserver(self, selector: #selector(self.reachabilityChanged(_:)), name: ReachabilityChangedNotification, object: reachability)

        do {
            try self.reachability?.startNotifier()
        } catch {
            print("Unable to start Notifier")
        }

        return true
    }

    func reachabilityChanged(_ note:Notification){
        print("is in here")
        let reachability = note.object as! Reachability
        if reachability.isReachable{
            print("is Reachable")
            // self.amConnected.text = "YES"
            //self.amConnected.fadeOut(duration: 2.0)
        }else{
            print("IsNotReachable")
            //self.amConnected.text = "No"
            //self.amConnected.fadeIn(duration: 2.0)
        }
        print("Changed status")    
    }

推荐答案

Swift 3中的可达性进行了一些更改.下载最新的Reachability.swift文件并将其添加到您的项目中. 链接

There are some changes in Reachability in Swift 3. Download the latest Reachability.swift file and add that in your project. Link

对于 Swift 2.x 代码,请此处

Swift 3.x代码

现在在您的AppDelegate中获取一个Reachability类对象

Now in your AppDelegate take a Reachability class object

private var reachability:Reachability!

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    //Network Reachability Notification check
    NotificationCenter.default.addObserver(self, selector: #selector(reachabilityChanged), name: ReachabilityChangedNotification, object: nil)

    self.reachability = Reachability.init()
    do {
        try self.reachability.startNotifier()
    } catch {
    }
    return true
}

reachabilityChanged方法定义

//MARK:- Network Check
func reachabilityChanged(notification:Notification) {
    let reachability = notification.object as! Reachability
    if reachability.isReachable {
        if reachability.isReachableViaWiFi {
            print("Reachable via WiFi")
        } else {
            print("Reachable via Cellular")
        }
    } else {
         print("Network not reachable")
    }
}

无论何时用户从Wifi切换到蜂窝,反之亦然,或者当网络连接和断开(反之亦然)时,总是会调用此方法.
在我的情况下工作正常.

This will be always called whenever user switches from Wifi to Cellular and vice versa or when Network Connects and Disconnects and vice versa.
Working fine in my case.

有关中的更多详细信息,请阅读文档. >迅速进行3项重大更改部分

Read the documentation for more details in Swift 3 breaking changes section

为您制作样品

https://www.dropbox.com/sh/bph33b12tyc7fpd/AAD2pGbGgW3Un ?dl = 0

这篇关于我的可达性通知程序只能被调用一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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