Does a background iOS app receive notification that the display is going to sleep? - iphone

My specific requirement is an app that is in the background, being notified that the display is about to go to sleep or that the device has or is about to reach it's idle timeout - and then waking up and executing some (brief) piece of code.
I have found reference to notifications that an app is being put in the background or suspended here:
http://developer.apple.com/library/ios/#DOCUMENTATION/iPhone/Conceptual/iPhoneOSProgrammingGuide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html
And there seems to be a way of detecting on OSX:
http://developer.apple.com/library/mac/#qa/qa1340/_index.html
So, can a background iOS app receive notification that the display is going to sleep and execute just before it does?

No, thats not possible on iOS. I suggest you file a bug report at bugreport.apple.com and explain to them why you need such a feature, although this isn't a guarantee that such a thing will come. But if more people request this feature, the likelier it gets implemented.

Related

hide voip notification ios swift

Whats the correct approach for incoming calls on iOS. I use voip notification and e.g. when somebody is calling to me and I am not answering then after 30 sec I finish call and I send next voip notification (cancel notificaiton) to remove notification about call. The problem is that since iOS 13 we have to report all of voip notification. What should we do in this situation because I can't send voip notification (because I stop getting notification later due to apple politics) if somebody don't answer or when caller (outside of app) finish the call first then calle (app) have to get signal that call is terminated and to finish the call. How we should inform app that call is closed (if we answered or nope)? Maybe is there a way to report voip notification without opening callkit screen and terminating it automatically?
I think there are a couple of things that you can do here,
1/ I didn't understand why you are not able to use another VoIP to stop ringing, you should be able to do it. If there is already call kit presented in the system from your app you don't need to present another one, the system is smart enough to understand this and won't blacklist you. And in your case, since your phone is still ringing you should be able to send another VoIP without any problem, if this not working for you I suggest that, you should make sure you have an active call.
2/ You can create your own timeout mechanism, since app is awake and woke up with call kit OS won't terminate your app even if it is in background, so create your own GCD timeout or any other similar solution and use it.
3/ If you don't want to use VoIP at all, then you can use alert or background notifications however delivery is not guaranteed for these notifications, especially for background notifications.

Is it possible to have my closed app run code in iOS?

Let's say that my app in iOS is closed. Would it be possible to set it up so that it runs a piece of its code after a certain period of time?
You can have your app schedule push notifications, but you can't schedule it to run specific code unless it's running.
Short answer: no, you can not. At the most, your application will keep running in the background for 11 minutes, if you request the extra time (check the documentation for beginBackgroundTaskWithExpirationHandler on the UIApplication class). There are some things you can do: Schedule a local notification, which the user can use to launch the app, or use push notifications to remind the user of something he needs to know.
iOS allows background processing for the following reasons:
- location dependent functionality
- background audio playback
- voip
You can find out more from Apple's multitasking documentation

Request to server when app in background

I need that my app will send some data to server every six hours for example. Purpose is that it will send request to server even when app in background. As I know only thinks GPS, Music, Push Notifications work on background. Also, as I know UIApplication method beginBackgroundTaskWithExpirationHandler: works not for a long time after app goes on background. Guys, anybody have idea how to implement this? Thanks a lot!
It's simply not possible within the limits of the current iOS SDK. The only kind of apps that can update their content regularly from the background are Newsstand apps and for them, the interval is 24 hours AFAIK and the entire updating process is largely triggered by Apple.
Unless your app falls into one of the categories you mention, the short answer is you can't. The only exception is for Newsstand apps.
But: what data would you be sending to the server if your app isn't running? If you send data to the server when something happens then the server will always be up-to-date. If the user isn't running the app then, by definition, nothing has happened and the server is still "in sync" with the client. (Yes, this potentially makes the server harder to code.)

If NSTimer does not work when an iOS app is in the background, how can I get my app to wake up periodically?

I have noticed that some apps e.g. Skype run in the background.
I would like to have my app run in the background also, waking up every 1 seconds to update some data and then going to sleep again.
How can I do this?
I gather that NSTimer's do not work in the background.
You can't. Voice-over-IP apps get a special exception for this, basically the system manages a network socket for them and wakes them up if there's data. There's no way to do the same with a timer.
By the way, waking your app every second, your battery wouldn't last half a day.
See Executing Code in the Background
You would need to use push notifications to push information to the app (like the Facebook or Skype app). See this documentation from Apple on how to use push notifications.
Not every second though, that would drain the battery. Other than that, there isn't really a way unless you are running a VoIP app.

how to keep getting "accelerometer:didAccelerate:" when app in background

How can my app keep getting a call to its "accelerometer:didAccelerate:" method of UIAccelerometerDelegate IF another app interrupts my app, such as the iPhone user receiving a phone call?
My app measures motion, but stops if suspended by the user, or if interrupted by another app.
I know this has been difficult in the past, but maybe there is something new in iOS.
Thanks!
You can't, apple standards are to block the app when a proccess pops in, It freezes the app at the moment it was interrupted. If finishes the last proccess it started in the background then it freezes the app. If you find a way around it i'd love to know it. Here is something that might help you to find the solution stackoverflow post similar to your question