Programmatically force an iOS 7 app to suspend? - ios

I am trying to force my app to automatically enter the "Suspended" state programmatically, so I can do testing on Core Bluetooth restoration.
I have tried calling
[[UIApplication sharedApplication] performSelector:#selector(suspend)];
but this merely sends it into the background. Am I doing it wrong? Is there a better way? Or is it impossible?
I would like to remind everyone exactly what it means to have an app be suspended, as there is always confusion on the terminology:
(source: apple.com)
An app in the background will get indefinitely suspended if the system needs memory, but Core Bluetooth's restoration can send an app back into the background state temporarily.

I don't know how to do this programmatically, but what I do is to press the home button, then launch a number of heavyweight apps like Safari and graphics-intensive games. The memory pressure causes the system to terminate your app pretty quickly. Core Bluetooth should then initiate the restore process and re-launch your app when it detects activity from a peripheral you are connected to/have asked to connect to.

Related

How to get screen lock/unlock events on the iPhone when app is suspended?

I have a requirement where i need to track the iphone device state like if device is locked or unlocked. I was able to track these events when the app is running in foreground or background. But i also need to track the same when the app is suspended. Something like tracking the user location in background when the app is suspended. But i don't need to track the user location but only the device state.
Please suggest me some steps to solve this issue in objective-c.Thanks in advance.
You can not perform any operations once your app is in suspended state and you can not prevent your app from getting suspended unless you are using one of the background capability mode mentioned in this apple doc
So what you are looking for is not possible if you are not using either of background modes allowed by apple.
EDIT
Even if you go on and enable one of background mode like background audio, your app is likely to be rejected during review process as reviewer will see you do not have a valid reason to use that particular background mode.

Return app to background for bluetooth event handling after manual app termination

I'm implementing a programmable BLE button. I've managed to implement the
scenario when the app is terminated by the system and then when I press the
button the bluetooth central manager restores its state with the centralManager(_:willRestoreState:):
method. The peripheral is connected and all the services are visible. But this happens only after I
emulate the app termination via kill(getpid(), SIGKILL).
But. When I terminated the app via double clicking the Home button and swiping the app off the screen
the centralManager(_:willRestoreState:): is not called and the app is not returned to the background.
Why is that? It turns out that termination of an app with the ios itself and the manual termination
of an app are different things.
So how can I make so that the app is returned to background not only after ios app termination but
also after manual termination?
This is desired behaviour. IMO there is no way to work around. If user swipe up your application in the app switcher, that means that he/she don't wont to be bothering by app anymore. This is Apple way to respect user's privacy.
The same thing with remote notifications.

How does Moves app work on iOS

Moves app on iOS geolocates your movements throughout the day even when the app is inactive. In addition to this it also appears to analyse core motion data to identify if you have walked, cycled or taken another form of transport (walking with your phone in a suitcase on wheels reports as a cycle).
What iOS methods and techniques can the Moves app use to continuously capture this data without the application having been open? Geofencing? Background processes?
You can register an app to continue operating in the background. Have a look here at the Background Execution and Multitasking section. http://developer.apple.com/library/ios/#DOCUMENTATION/iPhone/Conceptual/iPhoneOSProgrammingGuide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html

Keep app running while iOS device locked?

I have an app that makes heavy use of video out. In a typical use-case, I'll have an iPad connected to an external monitor. I just want the external monitor on; the iPad display does not need to stay on.
The ideal case would be for someone to connect to an external monitor, then lock their iPad. But that pauses my app. (Currently, I'm calling setIdleTimerDisabled to keep the iPad from locking up and pausing my app.)
I'd like to give the user the option of locking the iPad, but still having my app running and sending images to video out. (Note: I'm not talking about keeping my app running when it's not in the foreground. I just want to keep it running while it's in the foreground, but the device is locked.)
Is this possible?
I would say no, it is not possible. Here's why:
The docs read:
Pressing the Sleep/Wake button is another type of interruption that causes your app to be deactivated temporarily. When the user presses this button, the system disables touch events, moves the app to the background but sets the value of the app’s applicationState property to UIApplicationStateInactive (as opposed to UIApplicationStateBackground), and finally locks the screen.
Something interesting to note in the docs above is that a bit further down under "What to do when an interruption occurs" Apple recommends that you stop doing certain tasks.
In response to this change, your app should do the following in its applicationWillResignActive: method:
Stop timers and other periodic tasks.
Stop any running metadata queries.
Do not initiate any new tasks.
Pause movie playback (except when playing back over AirPlay).
Enter into a pause state if your app is a game.
Throttle back OpenGL ES frame rates.
Suspend any dispatch queues or operation queues executing non-critical code. (You can continue processing network requests and other time-sensitive background tasks while inactive.)
This tells me that Apple doesn't want or expect your app to be doing much of anything in this state, other than preparing to be fully backgrounded.
On a related note here's a thread that shows how to determine whether you've hit the Sleep/Wake button or not:
Is it possible to distinguish between locking the device and sending an app to background?

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.

Resources