After programatically sending user to the settings screen, there's a back-to-app button in left upper corner:
Tapping this button makes the user go back to my app. However, at that point Application calls its delegate with the same methods that are being called when we get back from background:
applicationWillEnterForeground
and
applicationDidBecomeActive
Meanwhile, I need to distinguish whether user came back to the app by tapping this particular "back-to-app" button, or by simply entering the app after sending it to background in any other way. Is this somehow possible?
Do you develop two apps that you want to connect that way?
There is far more way to leave your app then that you described:
I may miss sth, but this list I created in favor for showing that, distinguish between this action can be very hard. Even if you will handle one of the action is not necessarily handle all the other actions.
Will help if you'll tell more about the use case that you have or problem that you're trying to solve.
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
I believe, there is no way to distinguish by default.
My suggestion is, if you are focusing for a particular settings entry change, just compare the new setting's value with the old one in the applicationDidBecomeActive
. If there is a change, then you can distinguish the flow. However, If there is no change, you can't.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With