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

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.

Related

background operation

Is it possible to put your app in the background, and have a counter that once expires, wakes up the app and have the app does some action? I know it's basic, but I just cant seem to have it work. Where do I put this counter + action? Under app did enter background?
Thanks for the help.
I don't think that this works in general:
You are allowed only to run in the background for specific tasks:
Apple doc tells which tasks that are:
One of that tasks is receiving GPS messages.
As long as you have GPS enabled and your app configured that it uses GPS for background, your app stays alive in the background.
If you disable GPS some time later it will suspend, and not wake up till the user activates it.
So to realize your problem you have to stay active in the background (e.g by reading GPS).
You can start the timer in AppDelegate:applicationDidEnterBackground or similar
If you need more time to shutdown, you explicitly can request for more time, there is one method for that. I dont know what happens if you request more at regulary intervals

Creating an alarm application for the iPhone

I have an idea for a unique alarm application on the iPhone. But at the moment the only way I can see of initiating the alarm is by leaving the app running all night getting it to poll for the current time. Is there anyway to make an app "wake up" or initiate at a certain time. I know I can use push or local notifications but they require user input before loading my app. Thinking about it I could leave my app on all night, but literally doing nothing (saving battery if not being charged) and then subscribe to a local notification for the alarm itself.
Alternatively, can I make the iPhone run my app when an alarm sounds so I don't have to deal with alarm settings at all?
No, your only option is to use either Local or Push notifications.
I think using event kit framework add event in default iPhone calender.

iPhone App Architecture

I want to update my sqlite database in my iPhone app in every 2 hrs from the server.
Is there any way to call web services from the background even the app is not active?
You can't do this. The best you can do is sync when the app starts up. And if its backgrounded, you wait until they bring it to the foreground and can sync if it's been longer than 2 hours since the app was last in the foreground.
Apple won't let you do these battery draining and CPU stealing things. And really, you don't even need to. If your app isn't using the data, then it doesn't need to be up to date.
You can't run your app in background for doing this. You're app is only allows to run in background for doing Audio Playback, VoIP and Task Completion (till 10min after going to background). But you could do the update once in 2hours when the user starts the app? doing something with push notification to inform the user? But you can't just run something every two hours in background.

How to run a ~30sec process in the background every hour (iphone app)

I have an iphone app that has a 30second process that does some network IO. Basically, while the app is in the background, i want this process to run every hour (actually once a day, but if it fails i want it to re-run in an hours time).
With the background features of ios 4, is this possible? If so, how? What are the limitations that i'll come up against?
Thanks so much!
Take a look at Apple's documentation about running code in the background.
http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/BackgroundExecution/BackgroundExecution.html
There are few different ways of approaching backgrounded tasks. The only apps that can have fully backgrounded processes are "audio", "voip" and "location" apps, and this needs to be declared in the Info.plist.
If your app is not of this type, you'll probably find it difficult to do what you want easily. There are methods which allow you to keep your app alive in the background for a finite period of time (also at that link), but eventually your app will be shut down.
Local Notifications will only prompt the user to open the app - do you really want to have an alert pop-up on the phone every 30 seconds?
I was making some kind of similar research, have a look at this SO answer in case you didn't manage to find it before. Applications like DataMan or Data Usage must have some sort of periodic code execution in the background, so I'm not 100% convinced that what you're asking for is impossible..
I believe that Using Local notifications will help....
check following....
http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html#//apple_ref/doc/uid/TP40008194-CH103-SW1
An application can create and schedule a local notification, and the operating system then delivers it at the schedule date and time. If it delivers it when the application is not active in the foreground, it displays an alert, badges the application icon, or plays a sound—whatever is specified in the UILocalNotification object. If the application is running in the foreground, there is no alert, badging, or sound; instead, the application:didReceiveLocalNotification: method is called if the delegate implements it.
The delegate can inspect the properties of the notification and, if the notification includes custom data in its userInfo dictionary, it can access that data and process it accordingly. On the other hand, if the local notification only badges the application icon, and the user in response launches the application, the application:didFinishLaunchingWithOptions: method is invoked, but no UILocalNotification object is included in the options dictionary.

How long can an iPhone app live in the background?

I can't seem to find a clear answer to this-- I'm spec'ing out an iPhone app that I'd like to have live in the background and notify the user at certain periods throughout the day. So the user would launch the app in the morning and then continue to use their phone, then every few hours the app would pop open a notification dialog.
Will my app ever be shut down (automatically) by the OS? Or will it just live forever, notifying user when it needs to?
thanks,
Eric
Basically there are three kinds of running in the background on iOS 4:
Running in the background to "finish" stuff (e.g. upload a posting or a picture, finish processing something etc.). You ask the OS to grant you extra time after the user switches to another app, and it will tell you how much time you got. You can't run in the background for an indefinite time.
Running in the background to do specific stuff: VoIP, tracking location (e.g. for GPS navigation), or playing audio. You can only do the stuff that you told the OS you would do in the background.
Local notifications (UILocalNotification). From your description, this is what you're looking for. You're not actually running, you just schedule notifications, and when it's time to notify the user, they'll be notified and can go to your app. If you need to notify the user dynamically (i.e. you don't know ahead at what times they need to be notified and it's not location or VoIP triggered), you might want to look into push notifications.
Apple has a good overview here:
https://developer.apple.com/documentation/uikit/app_and_environment/scenes/preparing_your_ui_to_run_in_the_background/about_the_background_execution_sequence