12

After programatically sending user to the settings screen, there's a back-to-app button in left upper corner:

enter image description here

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?

jscs
  • 62,161
  • 12
  • 145
  • 186
user3581248
  • 816
  • 9
  • 18
  • see https://stackoverflow.com/questions/13447101/bring-previous-app-back-to-the-front-when-user-is-done-with-my-ios-app – Burnie777 Jun 22 '17 at 11:42
  • 1
    If you are "programmatically sending the user to the settings screen", couldn't you simply set a flag in your app before that? Then in `applicationDidBecomeActive`, check for that. – dfd Jun 22 '17 at 12:13
  • @dfd user can then leave the settings and get back to app any time later. Then the flag is still set to true, while it shouldn't be anymore – user3581248 Jun 22 '17 at 13:39
  • I think your requirement should be fulfil with the way mentioned in @dfd comment. – vivek bhoraniya Jul 01 '17 at 11:06

3 Answers3

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.

Shamsudheen TK
  • 28,767
  • 9
  • 64
  • 96
  • Unfortunately this solution won't work for me, since I'm sending the user to auto-lock settings, and those, from what I know, can't be read from the code... – user3581248 Jun 22 '17 at 12:02
3

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:

  1. User taps home button once
  2. User taps home button twice
  3. User press power button while app is still in a foreground
  4. On 3D-touch enabled devices user do 3D touch in leading edge.
  5. User uses "Back to the app" thing you described
  6. User gets notification and pick-pop it
  7. User goes to the other app from notification
  8. User opens notification center and do action there
  9. User opens control center and do some action there
  10. User use sharing functionality or hyperlink inside your app that can trigger other apps.

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.

Jakub
  • 12,851
  • 13
  • 75
  • 130
  • The requirements for the app I'm developing include a flow that allows the user to change auto-lock time (it was developed initially on Android where apparently you can do this from code). Obviously, there's no option to do this exactly as it is on Android, so my idea was to programatically send the user to settings screen. BUT there's another requirement - everytime we get back to the app, we should get back to the start screen, since other screens might contain confidential data. But in scenario described above I want to get back not to the start screen, but to the app settings' screen. – user3581248 Jun 26 '17 at 10:21
  • 1
    In that case, you can move the user to start screen before the user moves to the settings screen. This way you can achieve your goal. – Jayeshkumar Sojitra Jun 28 '17 at 10:54
  • +1 for @JayeshSojitra comment. user381248 - Android *ports* are never 100%. Worse, at least to me (and probably others), is that you **never** put this type of detail in your question. PLEASE, this belongs as part of your question! (Not the Android part - that can help - but the need to return to the home screen *after* sending an iOS user to Settings. That's *extremely* important.) – dfd Jul 01 '17 at 14:22
2

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