Application event notification (e.g. UIApplicationDidBecomeActiveNotification) handling in Swift.
- You don't have to use
NSNotificationCenter
. - You can catch event(s) only you want to.
- You can do
subscribe/resume/suspend/dispose
at anytime.
First, you create ApplicationEventObserver
instance.
The instance obverves UIApplication events untill release(deinit).
(e.g.)
class ViewController: UIViewController {
private lazy var appEventObserver = ApplicationEventObserver()
...
}
Second, subscribe event(s) you want to catch.
For example, you want to catch UIApplicationDidBecomeActive
event,
check returned value event
's type in handler(closure).
func viewDidLoad() {
super.viewDidLoad()
appEventObserver.subscribe() { event in
switch event.type {
case .DidBecomeActive:
print(event.type.notificationName)
default:
break
}
}
}
Also, you can suspend/resume anytime.
If instance's state is suspended, stop observing all events until call resume()
(e.g.)
func showPopup() {
appEventObserver.suspend()
}
...
func closePopup() {
appEventObserver.resume()
}
ApplicationEvent
has two parameters.
- type :
ApplicationEventType
- value :
NSNotification
instance's userInfo value if the instance has. Provide asAnyObject?
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "notified:",
name: UIApplicationDidBecomeActiveNotification,
object: nil
)
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "notified:",
name: UIApplicationWillChangeStatusBarFrameNotification,
object: nil
)
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
}
private extension ViewController {
@objc private func notified(notification: NSNotification) {
switch notification.name {
case UIApplicationDidBecomeActiveNotification:
print(notification.name)
case UIApplicationWillChangeStatusBarFrameNotification:
let rect = notification.userInfo?[UIApplicationStatusBarFrameUserInfoKey]
print(notification.name,rect)
default:
break
}
}
}
import UIKit
import ApplicationEventObserver
class ViewController: UIViewController {
private lazy var appEventObserver = ApplicationEventObserver()
override func viewDidLoad() {
super.viewDidLoad()
appEventObserver.subscribe() { event in
switch event.type {
case .DidBecomeActive, .WillResignActive:
print(event.type.notificationName)
case .WillChangeStatusBarFrame:
if let v = event.value {
print(event.type.notificationName, v)
}
default:
break
}
}
}
}
So simple !!:star:
- Supported events list below:
Notification Name | ApplicationEventType |
---|---|
UIApplicationDidFinishLaunchingNotification | .DidFinishLaunching |
UIApplicationWillEnterForegroundNotification | .WillEnterForeground |
UIApplicationDidEnterBackgroundNotification | .DidEnterBackground, |
UIApplicationWillResignActiveNotification | .WillResignActive, |
UIApplicationDidBecomeActiveNotification | .DidBecomeActive, |
UIApplicationDidReceiveMemoryWarningNotification | .DidReceiveMemoryWarning, |
UIApplicationWillTerminateNotification | .WillTerminate, |
UIApplicationSignificantTimeChangeNotification | .SignificantTimeChange, |
UIApplicationWillChangeStatusBarOrientationNotification | .WillChangeStatusBarOrientation, |
UIApplicationDidChangeStatusBarOrientationNotification | .DidChangeStatusBarOrientation, |
UIApplicationWillChangeStatusBarFrameNotification | .WillChangeStatusBarFrame |
UIApplicationDidChangeStatusBarFrameNotification | .DidChangeStatusBarFrame |
UIApplicationBackgroundRefreshStatusDidChangeNotification | .BackgroundRefreshStatusDidChange |
UIApplicationWillChangeStatusBarOrientationNotification
UIApplicationDidChangeStatusBarOrientationNotification
→ You can get orientation value.
UIApplicationWillChangeStatusBarOrientationNotification
UIApplicationDidChangeStatusBarOrientationNotification
→ You can get rect value.
- iOS 8.0+
- Xcode 7.0+(Swift 2+)
- Just add the following line to your Cartfile:
github "sgr-ksmt/ApplicationEventObserver"
- Run
carthage update
on Terminal. - Add the framework as described. Details: Carthage README
Just add the following line to your Podfile:
pod 'ApplicationEventObserver'
- Run
pod install
on Terminal.