How to launch an iOS app into the background in simulator? - ios

In the documentation (App States and Multitasking) which Apple provides:
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. When launching into the background, the system still loads your app’s user interface files but it does not display the app’s window.
How to simulate to launch an app into the background in iOS Simulator?
If an app launched into the background, will the UIApplicationDelegate method -applicationDidEnterBackground: be called?

No, applicationDidEnterBackground: won't be called in this case.
You can't simulate real launch into background behaviour if Xcode is attached.
(But you can simulate launch with UIApplicationLaunchOptionsLocationKey key using location simulation)
I tested significant location change API on real device and collected logs after tests. Results:
application:willFinishLaunchingWithOptions: is called with UIApplicationLaunchOptionsLocationKey key.
But applicationDidEnterBackground: is not called.

You just have to launch your app and then go to home screen in simulator - press cmd + shift + H and the app is in background state and - (void)applicationDidEnterBackground:(UIApplication *)application in appDelegate is called .

Related

applicationWillResignActive called without reason on iOS 10 ( swift 3 )

When I launch my app on iOS 10, I can see that after a short delay, the Appdelegate function -> applicationWillResignActive() is called.
There is no reason for that. The app is still active and in foreground state when it occurs and the app continues to run normally.
Please see above the lifecycle of my app :
--> Click on the app icon
App launch
application --> didFinishLaunchingWithOptions
application --> applicationDidBecomeActive
RootViewController --> viewDidAppear
application --> applicationWillResignActive <-- issue !
application --> applicationDidBecomeActive <-- again ??!!
at this point, the app is still running with no error
This sequence is repeated each time I open the app.
It looks as if something forces my app to quit the foreground state for an ultra short delay.
Usually, applicationDidBecomeActive is called when the app displays an alert ( for example if the app requires an user's permission to access the camera ) or when the user clicks on the home button.
1 - It only occurs when the app starts in landscape mode
2 - It only occurs on iPhones and not on iPads
3 - The problem does NOT occur on an iOS 9 device
Did anyone noticed this problem ?
The problem is, it calls second time after dismissing system services alert (location, push notifications, photos)
So the only way to handle it is to use variable in AppDelegate which increments each time some system alert shows and decrements in applicationDidBecomeActive, so you call your code only if value of this variable is 1.
Another interesting thing is that applicationDidEnterBackground doesn't call when system alert shows, thus we can use this info to decide whether we should call our code in applicationDidBecomeActive or not (but still, it can be less reliable solution)

AppDelegate method applicationWillEnterForeground didn't work (worked with long delay)

Our application use UIBackgroundMode "voip" so it can be launch in background.
In our case application was launched in background by iOS and application:didFinishLaunchingWithOptions was called. Then user immediately tapped to the app icon. We don't know when it actually occurred but its look like after several seconds from background launch. After that user made some work in application, interacted with UI, but application wasn't got applicationWillEnterForeground method invoke and notifications. This method was called after 30 minutes after user worked with application. And all of this time UIApplication applicationState value was UIApplicationStateBackground.
Why did applicationWillEnterForeground was called so later?
There is not steps to reproduce this situation because it is rare case. iOS Version is 8.1

App background - foreground states

Is there a way i could stop an app from going into background ? Or is there a way in which i could bring my app to foreground if it did go into background ?
I'm making a showcase app for a client and the app must always run on the iPad without interaction from the user.
You can force your app to stay active using following code
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
But if you press home button of device, the app will go in background. You cannot force to stop this.
In this case you can use UILocalNotification to bring the app to the foreground if the device is locked, a notification appears, and the user unlocks the device or if user tap on the notification.
You can also fire a local notification whenever user going to background in
applicationDidEnterBackground: method

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

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.

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