I would suggest a generic solution related to solve similar problems detecting different launch Options (How our App is in Active state (Running))
Swift 2.3
In AppDelegate
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if let options = launchOptions{
print(options.description)
//These options will give the difference between launching from background or from pressing the back button
if (launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] != nil) {
//this indicates the app is launched by pressing Push notifications
}else if (launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey] != nil) {
//This indicates the app is launched on tapping the local notifications
}else if (launchOptions?[UIApplicationLaunchOptionsSourceApplicationKey] != nil){
//This indicates the App launched from a valid source e.g: on tap of Open App from App Store when your App is installed or directly from home screen
}
}
}
Reference:
Apple docs provide all the available launch options which can be detected
https://developer.apple.com/documentation/uikit/uiapplicationdelegate/launch_options_keys
Use the Power of delegate Protocol methods by adding Observers
https://developer.apple.com/documentation/uikit/uiapplicationdelegate
Swift 3 Equivalent:
//adding observer
NotificationCenter.default.addObserver(self,
selector: #selector(applicationDidBecomeActive),
name: .UIApplicationDidBecomeActive,
object: nil)
//removing observer
NotificationCenter.default.removeObserver(self,
name: .UIApplicationDidBecomeActive,
object: nil)
// callback
func applicationDidBecomeActive() {
// handle event
}
Similar Questions in StackOverFlow which my help you out:
Detect when "back to app" is pressed
How to detect user returned to your app in iOS 9 new back link feature?
Detect if the app was launched/opened from a push notification
Checking launchOptions in Swift 3