Keep iPhone app opened in background - ios

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)

Related

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

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/>

Reset View hierarchy iOS

I'd like to add some code to the app delegate that resets the view hierarchy back to the beginning.
My app is basically a demonstration mockup, and I'd like EVERYTIME the app opens that it resets to the first view in the storyboard, and doesn't remember what page the user was on when they closed or 'minimized' the app.
I'm using iOS sdk 8.1, and Xcode 6.
Putting aside that it's actually quite bad user experience - it's very easy to do. You just need to specify that your app doesn't run in the background, and each time user closes app, next time - brand new copy will be launched.
Here is what you need to set in your project properties in Xcode:
If your'r info.plist Show raw keys/values option are enabled, the property is named UIApplicationExitsOnSuspend which you can get by right clicking on the empty space of info.plist properties table and selecting Add Row option as follows
and right after that you will be presented with following option,
where you can select second options which is Application does not run in the background.
Selecting the mentioned property and setting it to YES, the app opt out of background mode and it cycles between the not-running, inactive, and active states and never enters the background or suspended states and moved back to the not-running state. In other words, iOS will not preserved any states which allows the app to run as fresh on next launch.

iOS - show consistent alert at the top of the UI when backgrounding the app (like personal hotspot does)

I am creating an alarm clock app that requires some user action within the app in order to turn the alarm off. Below is a picture of what another app, Sleep Cycle, does when you turn an alarm on and press the home screen (i.e. background the app).
Here is an image link (I can't post an image yet, no rep despite my many attempts to answer people's questions today) for the effect I want to re-create.
Those that have used iPhone's Personal Hotspot and connected a device will notice that it is the same effect, where a notification appears at the top of the UI - pushing everything down by around 20-40 points. This is highly desirable to an alarm clock app as it encourages the user to keep the app in the foreground so that the app can easily be entered when waking up (instead of relying on the 30 second sound window allowed by local notifications)
Does anyone have any ideas on how to implement this functionality. I assume that it must go somewhere in the:
- (void)applicationDidEnterBackground:(UIApplication *)application
tag within the AppDelegate, but I'm not sure what exactly I need to be reading up on. So if anyone has a link to some relevant Developer Docs that would also be extremely helpful.
Many thanks for your help,
Ryan
There are a handful of built in 'background modes' that change the status bar's appearance depending on what functionality an app provides whilst it's in the background. The one you've identified (a red status bar) is triggered when an app records audio whilst in the background. I presume Sleep Cycle must be acting as though it records audio just for this purpose. Other background modes include VoIP (which I think uses a blue status bar). Check out Apple's documentation on supporting these various background modes
In your case, you'd want to add audio to the UIBackgroundModes property in your Info.plist file.
But note that it wouldn't be unreasonable for Apple to reject an app during review if it pretends to perform one of these background tasks but doesn't. For example, there have been apps in the past that tried playing a silent audio clip continuously in order to stay awake in the background - needless to say Apple got wise to this and the app in question had to change its behaviour.

iOS Application is not resuming where it left off?

I am facing the above issue and unsure why it is happening or how to fix it. When the app goes to the background and is later reopened, it always starts from the initial view.
I would like it to show the view that was shown when the app was dismissed - which is usually the default for iOS apps.
Can someone please explain why this might be happening and how I can resolve it.
Your app is probably killed while in the background to free it's resources for other tasks.
If applicable you can opt to use a background mode for your app so it keeps running in the background. This is possible if you require location updates, play audio or interact with bluetooth le devices.
By your description it's more likely you want to implement State preservation and Restoration (Programmig Guide)
Even if your app supports background execution, it cannot run forever. At some point, the system might need to terminate your app to free up memory for the current foreground app. However, the user should never have to care if an app is already running or was terminated. [...]
The state preservation system in UIKit provides a simple but flexible infrastructure for preserving and restoring the state of your app’s view controllers and views.
If you are on an iPhone older that the 3GS, this is normal. Apps are closed instead of backgrounded on these older models.
The other possible reason is that you've set
UIApplicationExitsOnSuspend=YES
in your Info.plist. In that case, your app will also always terminate when going to the background.

No camera picture when app returns from running in the background

I have an app using the camera. But each time the app has run in the background and you return the camera picture is gone.
Is there a trick i can make the app start over from the top each time it starts running in the foreground again?
Thank you
You can add the key UIApplicationExitsOnSuspend to the Info.plist file and set it's value to YES. The key is also known as "Application Does Not Run In Background" if you have the friendly key names enabled for Info.plist.
This will make your app behave as apps used to before iOS 4 where they did not suspend or multitask.

Resources