How to tell when user interacts with iOS location-aware lock screen app icon - ios

My app uses location services in the background and as a result it shows on the iOS 8 lock screen (see picture). Is there any way to tell when an app is brought to the foreground by the user sliding this button?
The app will necessarily be launched when this button appears -- since it's being awakened by the same location signal that is causing it to appear on the lock screen. Accordingly, application:didFinishLaunchingWithOptions: has a launch options dict with UIApplicationLaunchOptionsLocationKey = 1.
That's useful, but what I want to know is when the app is brought to the foreground due to interaction with the button. No relevant application delegate methods seem to be called in this case, except didBecomeActive: and didMoveToForeground: but these methods don't carry extra data.

Related

Swift Xcode - how to differentiate between going to lock screen and going to home screen?

When the app enters the background or WillResignActive, both functions are called in my SceneDelegate. How am I able to distinguish between when the user exits the app to the HomeScreen versus just locking their phone?
EDIT: I want to perform a different function in each scenario

Questions about app state after tapping on app icon after apps enters suspended state

I’m putting an app into background.
Assuming that I’m not doing anything to keep the app alive in the background, then the app goes through suspended state in a matter of 5 seconds. Right?
What happens if I then tap on the app icon? That’s not suppose to trigger a didFinishLaunch right? It will just bring me back to the last screen I was at and also trigger didbecomeActive & willenterforeground notifications. I won't be getting any other callback. Right?
Assuming there is no restart of the phone, point 2 is true even if there are hours between me tapping home and then tapping back the app icon. Right? Does it also persist device restarts but not force-restart?
The only time I won’t be brought back to the screen I was at (before hitting home) is if the device receives a memory warning and my app is flushed out of suspended state. At this point tapping on the app icon will result in didFinishLaunch. Right?
(I’m asking all of this because sometimes after putting the app in the background and tapping the app icon again (e.g. 10 minutes later), the app is going through it’s launch phase. Most of the time it just goes back to its previous screen)
I've already seen Will ios terminate the app running in background after a specific time? but that doesn't address all aspects I want.
It will just bring me back to the last screen I was at and also trigger didbecomeActive & willenterforeground notifications.
Right, if your app was not terminated in the background.
I won't be getting any other callback.
Not necessarily true. If you were summoned to the front by a local notification, for example, you'll also get an event about that.
Assuming there is no restart of the phone, point 2 is true even if there are hours between me tapping home and then tapping back the app icon.
Not necessarily. The app might well be terminated silently in the background.
Does it also persist device restarts but not force-restart?
Absolutely not. How can the app run when the device is off? Shutting down the app terminates every app.
I’m asking all of this because sometimes after putting the app in the background and tapping the app icon again (e.g. 10 minutes later), the app is going through it’s launch phase
It's not a matter of time. The watchdog process is constantly combing the suspended apps looking for the ones that take up too much memory so that other apps can run. You must not be surprised if yours is one of them.
You can come back to the front launching from scratch or by coming back to life from suspension; it's the most basic fact of iOS app life! You just need to accept it.
But there are lots of things you can do to reduce your chances of being terminated in the background. Giving up memory-consuming objects as you background is first on the list.

How can I tell if a user opened my iOS app from the home screen icon (without 3D touch)?

How can I tell if a user used the icon from the home screen? (as opposed to from a notification, for example)
application(_:didFinishLaunchingWithOptions:) signals the initial launch, but has no way to indicate whether it was opened from the home screen icon or opened from the app switcher after being terminated by the OS.
application(_:performActionFor:completionHandler:) is only for 3D touch actions.
applicationWillEnterForeground(_:) is better than applicationDidBecomeActive(_:) since it doesn't get called after opening the UI Notification Center, but still can be called for other reasons.

Stop Updating Locations on ios when user clicks stop button

I am using location services in my ios app for tracking purposes. The app tracks location in foreground as well as background, until the app terminates.
Here all the code is written in react-native java script except the beacon tracking API values which are being sent from native ios code through app delegate and location manager.
The app updates locations in all cases throughout the life cycle of app but sends beacon values to API only after a specific track interval. There is a stop button, on clicking of which the app stops sending location values to API but the location manager still continues to change lat long values.
What i want is, the clicking of stop button should stop the location manager updates and should start only when i click on 'start' button. This can be handled through app delegate methods but they are called only when any app transition happens like switching from foreground to background or vice versa.
They are not called if i switch between screens remaining on foreground only. And all the screens are handled through react code only, not in native ios.
Suggest something to control the location manager activities in the manner mentioned above, please.

When an iOS app directly enters the background state?

Can someone tell me a scenario when an iOS application directly enters the background state?.
Here I have quoted the lines from iOS Application Programming Document in multitasking section.
If your app is launched into the background instead—usually to handle
some type of background event—the launch cycle changes slightly to the
one shown in Figure 3-3. The main difference is that instead of your
app being made active, it enters the background state to handle the
event and then is suspended shortly afterward.
Added ...
In the iOS Application Programming Document if you see the figure 3.3 titled Launching an app into the background, the flow is like this User taps app icon -> main() -> UIApplicationMain() -> Enter background. Is there any chance when the app directly enters background when an user taps app icon. I interpreted the image like this. Is it correct?
Thanks.
One scenario for a background launch (App X)
X registered for location background mode in its Info.plist
X is run by the user, and registers for significant location changes while running
The user switches to another app Y, so X goes to background and is then suspended (it will be returned to background mode whenever there is a significant location change to handle, and then be suspended again)
The app Y eats lots of memory, so suspended applications (including X) get kicked out of memory
a significant location change comes in. Now X is launched into background.
Scenario
Lets say you had registered your application for Local/Push notification. Then your application will launch in background run some code which have written inside your applicationDidEnterBackgroud: delegate method and then it terminates immediately.
Check listing 2
Apple documentation
EDIT:
Applications might also find local notifications useful when they run
in the background and some message, data, or other item arrives that
might be of interest to the user. In this case, they should present
the notification immediately using the UIApplication method
presentLocalNotificationNow: (iOS gives an application a limited time
to run in the background). Listing 2-2 illustrates how you might do
this.

Resources