在应用未运行时从Reachability获取通知

时间:2016-03-29 15:34:37

标签: ios swift notifications reachability

我使用Reachability library来检查iPhone是否可以访问互联网。如果应用程序正在运行,它会收到通知。我的问题是如何让应用程序在没有运行时收到通知?

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(AutoCheckin.reachabilityChanged(_:)),name: ReachabilityChangedNotification,object: reachability)
        do{
            try reachability?.startNotifier()
        }catch{
            print("could not start reachability notifier")
        }

func reachabilityChanged(note: NSNotification) {

        let reachability = note.object as! Reachability

        if reachability.isReachable() {
            if reachability.isReachableViaWiFi() {
                logMessage("Reachable via WiFi: ")
                print("Reachable via WiFi: ")
            } else {
                logMessage("Reachable via Cellular: ")
                print("Reachable via Cellular")
            }
            self.pushLocalNotification()
        }else{
            logMessage("Network not reachable")
            print("Network not reachable")
        }
    }

如果有互联网,我还需要发送本地通知。

0 个答案:

没有答案