在Android系統中,只要系統配置發生變化(例如網絡狀態變化等),系統就會發送廣播。 這些廣播消息可以由我們的應用程序中的broadcastreceiver接收,並相應地更改應用程序的行爲,儘管我們的應用程序未處於運行狀態。 如何在iOS中實現類似的廣播接收器行爲?如何在iOS上獲取非運行狀態下的網絡變化通知
1
A
回答
0
您可以使用NSNotificationCenter
來監視更改。
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "batteryLevelChanged:",
name: NSUserDefaultsDidChangeNotification,
object: nil)
編輯1:
對於你的情況,你可以遵循的步驟和嘗試。
- 使用圖書館Reachability得到的 網絡的不斷變化的通知,並定義自定義通知功能。
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)
0
相關問題
- 1. 有關iOS上的網絡狀態更改的通知
- 2. 在iOS中如何在後臺運行狀態下獲取全局觸摸事件通知?
- 3. 如何獲取iOS 10通知中的應用程序狀態?
- 4. 使用BroadcastReceiver獲取網絡狀態變化
- 5. 網絡狀態更改通知
- 6. 通過PHP獲取網絡上一臺計算機的「狀態」
- 7. 如何在WiFi網絡狀態更改時收到通知?
- 8. 如何在網絡狀態更改時收到通知?
- 9. 在Windows,Linux,以及如何網絡狀態變化做在Python
- 10. MonoTouch的 - 入門通知時,網絡狀態改變
- 11. iOS UIPinchGestureRecognizer在UIGestureRecognizerStateEnded狀態下獲取與未知源的偏移
- 12. 獲取Windows 7的網絡狀態
- 13. iOS UISwitches在三種狀態下運行
- 14. 遠程通知在「未運行」狀態
- 15. iPhone通知當wifi網絡變化
- 16. 如何從運行在播放框架上的網站獲取http狀態
- 17. 如何在丟棄Wi-Fi網絡時在iOS中獲取可達性通知?
- 18. C++獲取服務網絡狀態linux
- 19. 如何在狀態欄高度變化時得到通知?
- 20. ng-repeat在狀態變化時運行
- 21. 關於Boost狀態圖狀態變化的通知
- 22. iOS - 當無線網絡或網絡收音機變爲活動狀態(不僅僅可用)時通知
- 23. 在Linux上獲取有關網絡接口更改的通知
- 24. CentOS服務器:如何檢測網絡狀態變化
- 25. Android的亮度變化通知(或狀態欄彙總通知)
- 26. iOS事件/通知網絡活動向上/向下/關
- 27. 變化的ImageButton狀態運行時
- 28. 如何獲取Qt中的系統網絡狀態?
- 29. 如何獲取網絡接口的狀態?
- 30. 如何從iOS Reachability Class獲取網絡連接通知的更改?
感謝您的回覆。可能是我的問題不清楚。我的期望是在網絡狀態改變時(例如,如果我們從WiFi更改爲數據卡,或者反之亦然),獲取本地通知或者警報或狀態欄上的通知,或者甚至是網絡狀態改變時的通知。即使我的應用程序不處於運行狀態,也應該觸發它。 – user3513902
瞭解,那麼你的請求已經有一個庫https://github.com/ashleymills/Reachability.swift。 – BobGao
謝謝@ BobGao,我研究了Reachability,它的確如預期的那樣。如果我的應用程序未處於運行狀態,它可以工作嗎?如果我更改網絡狀態,我應該在通知欄上收到通知。 – user3513902