我使用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")
}
}
如果有互联网,我还需要发送本地通知。