iOS app appears to still be in background after calling exit() - ios

I am trying to smoothly close down my app.
First I put the app in the background and tried to use exit(0) to close down the app:
//home button press programmatically
UIApplication *app = [UIApplication sharedApplication];
[app performSelector:#selector(suspend)];
//wait 2 seconds while app is going background
[NSThread sleepForTimeInterval:2.0];
//exit app when app is in background
exit(0);
My problem is, when I check to see what apps are running in the background, the app is still there. I thought exit(0) would remove it from the background.
It appears my app is going under recently used. Is there a way to programmatically remove it from that list?

You can't programmatically remove an app from the "recently used apps" list. When a user double-taps the Home button, the list of recently used apps is just that - a list of recently used app. It has absolutely nothing at all to with whether the app is fully terminated or suspended in the background.
Calling exit(0); simply terminates your app. But it was still recently used so it appears in the list when the user double-taps the Home button.

In XCode edit the info.plist adding the setting "Application does not run in background" with a value of YES to make your application exit every time:
Setting this will add UIApplicationExitsOnSuspend to info.plist:
<key>UIApplicationExitsOnSuspend</key>
<true/>

Related

applicationWillEnterForeground & UIActivityViewController

I have an issue with my obj-c iOS app.
I have a main view in it which is called to appear - when the app is reentering the foreground from the background - via applicationWillEnterForeground.
The thing is that when I use UIActivityViewController to open some content in another app (fx. Messages) this extern app opens on the top of my own and therefore, when I close the extern one, my own is not still in the foreground and applicationWillEnterForeground is not called. This causes a crash in my app.
I am looking for a possible alternative for applicationWillEnterForeground that tells the AppDelegate if the app is "in background" of another app via UIActivityViewController.
Any suggestions?

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 launch an iOS app into the background in simulator?

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 .

Keep iPhone app opened in background

As title says, i need to keep my iPhone app opened in background, i dont need to perform any task, just make it not start from new if not completely closed from multitasking (i.e. like native apps like Notes or Stocks do).
I added:
<key>UIApplicationExitsOnSuspend</key>
<false/>
to my Info.plist but it seems to be not enough. I placed NSLog to see what happens and i noticed that applicationDidFinishLaunching is fired everytime the app gets, opened no matter if it was closed or not from multitasking. Also applicationDidEnterBackground gets fired but i see that applicationWillTerminate immediately follows. applicationWillEnterForeground instead is never fired.
Any ideas on how can i make backgounding of my app to work?
Side notes: i'm compiling with theos directly on the iPhone.
You can keep your app running in the background with the iOS 7 Multitasking API.
https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html
You can set your app to run in the background by adding the UIBackgroundModes key to your Info.plist, settings it's value to an array, and adding app states to the array (link above)

iOS: Prevent app from launching into the background

I am currently building a music app which takes controls from the iOS7 control center. One of the features I noticed is that if I press the "play" control button, my app will start, even if it has been killed. This is not something which I want to happen, so I added return NO in didFinishLaunchingWithOptions if the app is launching in the background to prevent the initialization process.
Unfortunately, this does not change the fact that the app has still started, and didFinishLaunchingWithOptions is not called again (and nothing is initialized). Since I cannot have the app forcefully kill itself when I don't want it to start, is there any way to prevent the app from launching? I had thought that returning NO would have done the trick, but this does not appear to be the case.
I have found one of the main causes to this issue. It had to do with the audio session and the various notifications/delegates which the app was registered for. In the AppDelegate's applicationWillTerminate: I had to make sure to call:
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&audioSessionError];
And also unregister for any audio notifications (interrupts and route changes).
Do you want your app to run in the background at all?
If not, you can simply add a info.plist value so it doesn't enter background and just closes when a user hits the home button.

Resources