Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Back to app button handler

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?

like image 805
user3581248 Avatar asked Jun 22 '17 11:06

user3581248


3 Answers

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.

like image 92
Jakub Avatar answered Oct 21 '22 17:10

Jakub


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

like image 44
Yeswanth Varma Kanumuri Avatar answered Oct 21 '22 15:10

Yeswanth Varma Kanumuri


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.

like image 20
Shamsudheen TK Avatar answered Oct 21 '22 15:10

Shamsudheen TK