RxAppState
A collection of handy RxSwift Observables that let you observe all the changes in your Application's state and your UIViewController view-related notifications.
About
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:
UIApplication.shared.rx.didOpenApp
.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:
UIApplication.shared.rx.firstLaunchOnly
.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?
UIApplication.shared.rx.firstLaunchOfNewVersionOnly
.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?
UIApplication.shared.rx.appVersion
.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:
UIApplication.shared.rx.didOpenAppCount
.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:
rx.viewDidAppear
.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:
rx.viewDidAppear
.take(1)
.subscribe(onNext: { animated in
// do something
})
.disposed(by: disposeBag)
You can also directly bind you view controller's view state to another object:
rx.viewWillDisappear
.bind(to: viewModel.saveChanges)
.disposed(by: disposeBag)
Example
There is a simple example project to demonstrate how to use RxAppDelegate.
Requirements
iOS 8 or greater
Swift 5
Xcode 10.2
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
If you are using Swift 4.2 please use RxAppState version 1.4.1
Dependencies
RxSwift 5.0
RxCocoa 5.0
Integration
CocoaPods
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'] = '5.0'
end
end
end
Carthage
You can use Carthage to install RxAppState
by adding it to your Cartfile
:
github "pixeldock/RxAppState"
Swift Package Manager
You can use Swift Package Manager to install RxAppState
:
https://swift.org/package-manager/
Author
Jörn Schoppe,
joern@pixeldock.com
License
RxAppState is available under the MIT license. See the LICENSE file for more info.