RxAppState 1.4.1

RxAppState 1.4.1

LangLanguage SwiftSwift
License MIT
ReleasedLast Release Feb 2019
SPMSupports SPM

Maintained by pixeldock.




Pod Tries1
Test Targets65
powered by Segment


Pull Requests1


LOCLines of Code 275

Depends on:
RxSwift~> 4.3
RxCocoa~> 4.3


CI Status Platform Version Carthage compatible Swift Xcode License Twitter Blog

A collection of handy RxSwift Observables that let you observe all the changes in your Application's state and your UIViewController view-related notifications.


Application states

In almost every app there is some code that you want to run each time a user opens the app. For example you want to refresh some data or track that the user opened your app.

UIApplicationDelegate offers two methods that you could use to run the code when the user opens the app: applicationWillEnterForeground and applicationDidBecomeActive. But either of these methods is not ideal for this case:

applicationWillEnterForeground is not called the first time your app is launched. It is only called when the app was in the background and then enters the foreground. At first launch the app is not in the background state so this methods does not get called.

applicationDidBecomeActive does get called when the app is launched for the first time but is also called when the app becomes active after being in inactive state. That happens everytime the user opens Control Center, Notification Center, receives a phone call or a system prompt is shown (e.g. to ask the user for permission to send remote notifications). So if you put your code in applicationDidBecomeActive it will not only get called when the user opens the app but also in all those cases mentioned above.

So to really run your code only when your user opens the app you need to keep track of the app's state. You would probably implement something like this:

class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    var didEnterBackground = true
    func applicationDidEnterBackground(_ application: UIApplication) {
        didEnterBackground = true
    func applicationDidBecomeActive(_ application: UIApplication) {
        if didEnterBackground {
            // run your code
            didEnterBackground = false

This is not a big problem, but it is not a very elegant approach. And you have to set the inital value of didEnterBackground to true to run your code after the first launch (see above), even if the app never has been to the background. Call me picky, but I don't like that.

RxAppState to the rescue!
With RxAppState you can simply do the following:

    .subscribe(onNext: { _ in
        // run your code
    .disposed(by: disposeBag)

This runs your code whenever the user opens the app. It includes the first launch of the app and ignores the cases when the app enters active state without having been in background state before (like when the user just opened Control Center or received a phone call)

And there is more!
You want to show your user a tutorial when he first launches the app? And you only want to show it after the first launch and never again? No problem:

    .subscribe(onNext: { _ in
        // run your code
    .disposed(by: disposeBag)

You want to show your user what features are new when he opens the app for the first time after an update?

    .subscribe(onNext: { version in
        let previousAppVersion = version.previous
        let currentAppVersion = version.current
        // show what has changed between
        // the previous and the current version
    .disposed(by: disposeBag)

You want check the previous and the current app version each time the user opens the app?

    .subscribe(onNext: { version in
        let previousAppVersion = version.previous
        let currentAppVersion = version.current
        // run your code
    .disposed(by: disposeBag)

You want to keep track of how many times the user has opened your app? Simply do this:

    .subscribe(onNext: { count in
        print("app opened \(count) times")
    .disposed(by: disposeBag)

The cherry on top:
This code does not have to live in your AppDelegate. You could put it anywhere you like in your app! So don't clutter your AppDelegate with this code, put it somewhere else!

ViewController view-related notifications

You can also use Observables to subscribe to your view controllers' view-related notifications:

Do do something when your view controller's viewDidAppear: method is called you can do this in your view controller class:

    .subscribe(onNext: { animated in
       // do something
    .disposed(by: disposeBag)

If you want to do something only when the view appeared for the first time you can easily do it like this:

    .subscribe(onNext: { animated in
       // do something
    .disposed(by: disposeBag)

You can also directly bind you view controller's view state to another object:

    .bind(to: viewModel.saveChanges)
    .disposed(by: disposeBag)


There is a simple example project to demonstrate how to use RxAppDelegate.


iOS 8 or greater
Swift 4.2
Xcode 10

If you are using Swift 2.3 please use RxAppState version 0.2.0
If you are using Swift 3.x please use RxAppState version 0.3.4
If you are using Swift 4.0 please use RxAppState version 1.1.1
If you are using Swift 4.1 please use RxAppState version 1.1.2


RxSwift 4.3
RxCocoa 4.3



RxAppState is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "RxAppState"

If Xcode complains about Swift versions add this to the end of your Podfile:

post_install do |installer|
    installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
            config.build_settings['SWIFT_VERSION'] = '4.2'


You can use Carthage to install RxAppState by adding it to your Cartfile:

github "pixeldock/RxAppState"


Jörn Schoppe,
[email protected]

Twitter Blog


RxAppState is available under the MIT license. See the LICENSE file for more info.