標籤:
iOS通知中樞
它是iOS程式內部的一種訊息廣播機制,通過它,可以實現無參考關聯性的對象之間的通訊。通知中樞他是基於觀察者模式,它只能進行程式內部通訊,不能跨應用程式進程通訊。
當通知中樞接受到訊息後會根據設定,將訊息發送給訂閱者,這裡的訂閱者可以有多個
通知中樞原理
看完你應該明白通知中樞所做的事情了吧, 接下來我們就來看看通知中樞。
首先必須瞭解2個類:
// 這個類用來傳遞發送通知過程中傳遞資訊的載體NSNotification// 這是iOS中通知中樞的靈魂, 由該類實現了觀察者模式, 並給開發人員提供了諸如註冊、刪除觀察者的介面, 我們可以通過一個單例來獲得它的執行個體NSNotificationCenter
代碼實現註冊一個通知觀察者
// 註冊一個通知觀察者NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.refreshText(_:)), name: kRefreshTextNotification, object: nil)self: 觀察者selector: 收到通知執行的方法name: 通知名object: // 收到通知後執行的方法func refreshText(notification: NSNotification) { if notification.object is NSError { return } textField.text = String(notification.object!)}向觀察者發送通知
NSNotificationCenter.defaultCenter().postNotificationName(kRefreshTextNotification, object: "fffffff", userInfo: nil)kRefreshTextNotification: 通知名object: 參數userInfo: 其他參數
移除觀察者
deinit { NSNotificationCenter.defaultCenter().removeObserver(self, name: kRefreshTextNotification, object: nil) }
除了上面這種方式系統還有一個
NSNotificationCenter.defaultCenter().addObserverForName(kRefreshTextNotification, object: self, queue: NSOperationQueue.mainQueue()) { (notification) in}
但是在項目中並不多用, 所以就不詳細介紹了,其實用法差不多, 只不過用的時候需要注意循環參考問題, 比如你在內部用self, 這個self對應的class對象就不會被釋放
移除觀察者需要注意 不能用 NSNotificationCenter.defaultCenter().removeObserver(self, name: kRefreshTextNotification, object: nil), 需要用 NSNotificationCenter.defaultCenter().removeObserver(observer)
系統通知
除了自訂的通知名, 系統其實還有一部分通知名
UIDevice通知
UIDeviceOrientationDidChangeNotification // 裝置旋轉 UIDeviceBatteryStateDidChangeNotification // 電池狀態改變 UIDeviceBatteryLevelDidChangeNotification // 電池電量改變 UIDeviceProximityStateDidChangeNotification // 近距離感應器(比如裝置貼近了使用者的臉部)
鍵盤通知
UIKeyboardWillShowNotification // 鍵盤即將顯示 UIKeyboardDidShowNotification // 鍵盤顯示完畢 UIKeyboardWillHideNotification // 鍵盤即將隱藏 UIKeyboardDidHideNotification // 鍵盤隱藏完畢 UIKeyboardWillChangeFrameNotification // 鍵盤的位置尺寸即將發生改變 UIKeyboardDidChangeFrameNotification // 鍵盤的位置尺寸改變完畢
系統發出鍵盤通知時, 會附帶一下跟鍵盤有關的額外資訊(字典),字典常見的key如下:
UIKeyboardFrameBeginUserInfoKey // 鍵盤剛開始的frame UIKeyboardFrameEndUserInfoKey // 鍵盤最終的frame(動畫執行完畢後) UIKeyboardAnimationDurationUserInfoKey // 鍵盤動畫的時間 UIKeyboardAnimationCurveUserInfoKey // 鍵盤動畫的執行節奏(快慢)
app通知
// These notifications are sent out after the equivalent delegate message is called@available(iOS 4.0, *)public let UIApplicationDidEnterBackgroundNotification: String@available(iOS 4.0, *)public let UIApplicationWillEnterForegroundNotification: Stringpublic let UIApplicationDidFinishLaunchingNotification: Stringpublic let UIApplicationDidBecomeActiveNotification: Stringpublic let UIApplicationWillResignActiveNotification: Stringpublic let UIApplicationDidReceiveMemoryWarningNotification: Stringpublic let UIApplicationWillTerminateNotification: Stringpublic let UIApplicationSignificantTimeChangeNotification: Stringpublic let UIApplicationWillChangeStatusBarOrientationNotification: String // userInfo contains NSNumber with new orientationpublic let UIApplicationDidChangeStatusBarOrientationNotification: String // userInfo contains NSNumber with old orientationpublic let UIApplicationStatusBarOrientationUserInfoKey: String // userInfo dictionary key for status bar orientationpublic let UIApplicationWillChangeStatusBarFrameNotification: String // userInfo contains NSValue with new framepublic let UIApplicationDidChangeStatusBarFrameNotification: String // userInfo contains NSValue with old framepublic let UIApplicationStatusBarFrameUserInfoKey: String // userInfo dictionary key for status bar frame@available(iOS 7.0, *)public let UIApplicationBackgroundRefreshStatusDidChangeNotification: String@available(iOS 3.0, *)public let UIApplicationLaunchOptionsURLKey: String // userInfo contains NSURL with launch URL@available(iOS 3.0, *)public let UIApplicationLaunchOptionsSourceApplicationKey: String // userInfo contains NSString with launch app bundle ID@available(iOS 3.0, *)public let UIApplicationLaunchOptionsRemoteNotificationKey: String // userInfo contains NSDictionary with payload@available(iOS 4.0, *)public let UIApplicationLaunchOptionsLocalNotificationKey: String // userInfo contains a UILocalNotification@available(iOS 3.2, *)public let UIApplicationLaunchOptionsAnnotationKey: String // userInfo contains object with annotation property list@available(iOS 4.0, *)public let UIApplicationProtectedDataWillBecomeUnavailable: String@available(iOS 4.0, *)public let UIApplicationProtectedDataDidBecomeAvailable: String@available(iOS 4.0, *)public let UIApplicationLaunchOptionsLocationKey: String // app was launched in response to a CoreLocation event.@available(iOS 5.0, *)public let UIApplicationLaunchOptionsNewsstandDownloadsKey: String // userInfo contains an NSArray of NKAssetDownload identifiers@available(iOS 7.0, *)public let UIApplicationLaunchOptionsBluetoothCentralsKey: String // userInfo contains an NSArray of CBCentralManager restore identifiers@available(iOS 7.0, *)public let UIApplicationLaunchOptionsBluetoothPeripheralsKey: String // userInfo contains an NSArray of CBPeripheralManager restore identifiers@available(iOS 9.0, *)public let UIApplicationLaunchOptionsShortcutItemKey: String // userInfo contains the UIApplicationShortcutItem used to launch the app.// Key in options dict passed to application:[will | did]FinishLaunchingWithOptions and info for UIApplicationDidFinishLaunchingNotification@available(iOS 8.0, *)public let UIApplicationLaunchOptionsUserActivityDictionaryKey: String // Sub-Dictionary present in launch options when user activity is present@available(iOS 8.0, *)public let UIApplicationLaunchOptionsUserActivityTypeKey: String // Key in user activity dictionary for the activity type@available(iOS 8.0, *)public let UIApplicationOpenSettingsURLString: String// Keys for application:openURL:options:@available(iOS 9.0, *)public let UIApplicationOpenURLOptionsSourceApplicationKey: String // value is an NSString containing the bundle ID of the originating application@available(iOS 9.0, *)public let UIApplicationOpenURLOptionsAnnotationKey: String // value is a property-list typed object corresponding to what the originating application passed in UIDocumentInteractionController‘s annotation property@available(iOS 9.0, *)public let UIApplicationOpenURLOptionsOpenInPlaceKey: String // value is a bool NSNumber, set to YES if the file needs to be copied before use// Content size category constants@available(iOS 7.0, *)public let UIContentSizeCategoryExtraSmall: String@available(iOS 7.0, *)public let UIContentSizeCategorySmall: String@available(iOS 7.0, *)public let UIContentSizeCategoryMedium: String@available(iOS 7.0, *)public let UIContentSizeCategoryLarge: String@available(iOS 7.0, *)public let UIContentSizeCategoryExtraLarge: String@available(iOS 7.0, *)public let UIContentSizeCategoryExtraExtraLarge: String@available(iOS 7.0, *)public let UIContentSizeCategoryExtraExtraExtraLarge: String// Accessibility sizes@available(iOS 7.0, *)public let UIContentSizeCategoryAccessibilityMedium: String@available(iOS 7.0, *)public let UIContentSizeCategoryAccessibilityLarge: String@available(iOS 7.0, *)public let UIContentSizeCategoryAccessibilityExtraLarge: String@available(iOS 7.0, *)public let UIContentSizeCategoryAccessibilityExtraExtraLarge: String@available(iOS 7.0, *)public let UIContentSizeCategoryAccessibilityExtraExtraExtraLarge: String// Notification is emitted when the user has changed the preferredContentSizeCategory for the system@available(iOS 7.0, *)public let UIContentSizeCategoryDidChangeNotification: String // userInfo dictionary will contain new value for UIContentSizeCategoryNewValueKey@available(iOS 7.0, *)public let UIContentSizeCategoryNewValueKey: String // NSString instance with new content size category in userInfo// This notification is posted after the user takes a screenshot (for example by pressing both the home and lock screen buttons)@available(iOS 7.0, *)public let UIApplicationUserDidTakeScreenshotNotification: String// Extension point identifier constants@available(iOS 8.0, *)public let UIApplicationKeyboardExtensionPointIdentifier: String
iOS通知中樞