在Android中,每当系统配置发生变化(如网络状态的变化等)时,操作系统就会发送广播。这些广播消息可以在我们的应用程序中被广播接收机接收,并相应地改变应用程序的行为,尽管我们的应用程序没有处于运行状态。如何在iOS中实现类似的广播接收机行为?
发布于 2016-03-28 07:40:50
您可以使用NSNotificationCenter来监视更改。
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "batteryLevelChanged:",
name: NSUserDefaultsDidChangeNotification,
object: nil)下面是API引用:班级/
编辑1:
对于您的情况,您可以按照步骤进行尝试。
func reachabilityChanged(note: NSNotification) {
let reachability = note.object as! Reachability
if reachability.isReachable() {
if reachability.isReachableViaWiFi() {
print("Reachable via WiFi")
} else {
print("Reachable via Cellular")
}
} else {
print("Network not reachable")
}
}var notification = UILocalNotification()
notification.alertBody = "Notification message!!!" // text that will be displayed in the notification
notification.alertAction = "open" // text that is displayed after "slide to..." on the lock screen - defaults to "slide to view"
notification.fireDate = NSDate() // todo item due date (when notification will be fired)
notification.soundName = UILocalNotificationDefaultSoundName // play default sound
notification.userInfo = ["UUID": UUID, ] // assign a unique identifier to the notification so that we can retrieve it later
notification.category = "CATEGORY"
UIApplication.sharedApplication().scheduleLocalNotification(notification)发布于 2016-03-28 09:51:51
为了实现网络更改通知,必须使用Reachability类,下面是示例:
https://stackoverflow.com/questions/36257980
复制相似问题