在Android中,只要系统配置发生变化(如网络状态变化等),操作系统就会发送广播。 广播接收者可以在我们的应用程序中接收这些广播消息,并相应地更改应用程序的行为,尽管我们的应用程序未处于运行状态。 我如何在iOS中实现类似的广播接收器行为?
答案 0 :(得分:0)
您可以使用NSNotificationCenter
来监控更改。
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "batteryLevelChanged:",
name: NSUserDefaultsDidChangeNotification,
object: nil)
编辑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)
答案 1 :(得分:0)