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

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.

Related

iOS - Background Services when app is terminated

Do background mode functions like Background fetch and Location update work if the app is terminated? Or it only works if the app enters background?
Thanks
Yes, it works (most of the time), if you set up everything correctly and have the permissions. Your app need's to be launched at least 1 time, so it can subscribe to the updates.
For background fetch, set UIApplication.shared.setMinimumBackgroundFetchInterval(3600) at the didFinishLaunching method, implement the performFetchWithCompletionHandler method, and enable the Background Fetch in the Background Modes.
Pay attention to do it as quickly as possible, and call the completionHandler as soon as possible.
Read more on Updating Your App with Background App Refresh here
For notification updates, you must also set the allowsBackgroundLocationUpdates property of your CLLocationManager object to true, and enable the Location updates in the Background Modes.
Read more on Handling Location Events in the Background here
Background fetch works like, it allows the app to download the contents when it is background. If the app is terminated and gets some trigger to download content, it will actually wake up by doing silent-launch of the app in the background and download the contents. Please see the Apple description on this below.
Each of the preceding modes lets the system know that your app should
be woken up or launched at appropriate times to respond to relevant
events. For example, an app that begins playing music and then moves
to the background still needs execution time to fill the audio output
buffers. Enabling the Audio mode tells the system frameworks that they
should continue to make the necessary callbacks to the app at
appropriate intervals. If the app does not select this mode, any audio
being played or recorded by the app stops when the app moves to the
background.
Here, preceding modes refer to Background fetch, Audio and AirPlay, Location updates and other Background modes of the app.
Please refer Apple document on Background Execution. See Declaring Your App’s Supported Background Tasks for more info on different background modes.
Location update works differently. There are multiple Apple services available to fetch location.
Significant Location service: It works in all modes. Foreground, Background and even in terminated mode.
Standard Location service: It works only in FG and BG mode. It does not work when the app is in terminated mode.
On more details on Location in BG, please refer Handling Location Events in the Background document.
Hope it helps.
Background fetch and Location update work if the app is terminated? Or it only works if the app enters background?
It depends on which type of location service you have used in the project. Refer below analysis of all types of location services.
Standard location service: If you implemented standard location service then it will work only for background and foreground
state.
Significant location updates: If you implemented significant location updates then it will work for background, foreground and
terminate state as well.
Region Monitoring: If you implemented significant location updates then it will work for background, foreground and
terminate state as well.
Visits Location Service: If you implemented Visits Location Service then it will work for background, foreground and
terminate state as well.
Please refer below references.
Apple official doc
Raywenderlich article

Is there anyway to check when an app will be suspended?

Is there a universal time of iOS app suspension time (i.e when it goes out of background mode and terminates).
Background
The app is in the background and executing code. Most apps enter this
state briefly on their way to being suspended. However, an app that
requests extra execution time may remain in this state for a period of
time. In addition, an app being launched directly into the background
enters this state instead of the inactive state. For information about
how to execute code while in the background, see Background Execution.
Suspended
The app is in the background but is not executing code. The system
moves apps to this state automatically and does not notify them before
doing so. While suspended, an app remains in memory but does not
execute any code. When a low-memory condition occurs, the system may
purge suspended apps without notice to make more space for the
foreground app.
No, there is not any possibility to know about that, according to this:
Suspended: ...The system moves apps to this state automatically and does not notify them before doing so...
Link: https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/TheAppLifeCycle/TheAppLifeCycle.html

iOS background service vibrate phone

I have an app that runs in the background (location services). When the app is in the foreground it vibrates fine when I want it to, however, if its not in the foreground (in the multitasking view), the vibrate does not work anymore. Is this not allowed? Is there some way for my background app to give a vibrate alert? If not that what about a beeping sound?
Right now I am just doing something like this:
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
While importing:
#import <AudioToolbox/AudioServices.h>
If you enable background audio modes (as documented by Apple at https://developer.apple.com/library/prerelease/ios//qa/qa1668/_index.html) then vibration will happen when the app is in the background state. I just verified that adding the "App plays audio or streams audio/video using AirPlay" entry under the "Required background modes" property in the target's properties allows the vibration to work when the app is not in the foreground.
I found that checking 'Audio,AirPlay and Picture in Picture' within the 'Background Modes' of your apps Capabilities worked:
If you're trying to provide the user with a notification of something while your app is running in the background, consider delivering that information using a Local Notification (see UILocalNotification.)
Forcing the phone to vibrate is not a great user experience for a few reasons:
Some users (including myself) disable vibrate while in silent mode
iPads and iPod Touches do not vibrate
Users may not know what caused the vibration or sound and may not realize to open your app
Local notifications appear to the user in a manner that they can control and will recognize.
If your intention is something besides notifying the user, add that to your question & I'll try to help.
I'm pretty sure that you aren't able to vibrate the phone while it is in the background state. I've actually never seen an app do this.

Programmatically force an iOS 7 app to suspend?

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.

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?

Resources