How long can an iPhone app live in the background? - iphone

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

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

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.

Running apps in background?

Is it possible to keep a socket connection alive in background, to be able to push new data and alert users at all times?
The answer to this question is a definitive yes. If you are in the background state, then you can keep a connection open and process messages from a server.
Unfortunately the complexity here is that you don't have a lot of control over the state your application is in:
foreground - The user has tapped your icon and the app is running with the UI visible.
suspended - The user was previously running your app in the foreground, but suspended it by hitting the home button or receiving a call. Basically your app is 'freeze dried' and will remain inactive until it is resumed by the user (starting where it left off) or it is terminated by the OS (see below).
background - The app was previously running in the foreground but has moved to the background state as a result of something the user has done. Normally your app will move to the suspended state in this case, but there are things you can do as the developer to prevent the instant 'freeze dry' and go into the background instead (see below). Your app will also be in the background state if it is woken up for a significant change event.
terminated - Your app has been unloaded from memory and the next time it starts will be from scratch. This is what happens when you double click the home button and then tap the x next to your app icon. It moves the app from the suspended state into the terminated state. This will also happen if the OS decides it needs room for more recently running apps and your app has been suspended for a long time.
So obviously the trick here is how do I stay in the background state as a long as possible. There are a couple of ways to do this:
Beg for more time - You can get up to 10 minutes of additional background processing when your app is closed if you ask for it.
Use UIBackgroundMode - You can declare youself a voip, audio or location app by adding the corresponding UIBackgroundMode value to the pList. There are special requirements for these types of apps which you can check out here.
So these approaches are not without their own problems (getting approved on the store being one of them) and as such I tend to agree with the other answers that using push notifications is probably your best approach for notifying your users. With the notification improvements in iOS5 this is going to be the best user experience going forward.
You can keep a socket connection alive (or do whatever else you want) in the background for about 15 minutes after your app closes. There are also more specialized background processing modes (specifically, audio, voip, and location) which Apple supports if your app fits into one of their supported categories. See here.
If you want to keep sending the user notifications indefinitely, you want to use the Apple Push Notification Service. This allows your app to continue to receive notifications when it's not running, and it conserves resources since there's only one connection to the APN service at a time.
You can definitely alert users with local and push notifications.
And as far as I know, you can keep a connection open only for limited time.
Look here for more details.

Is there a safe way to schedule an alert for an calendar app?

I want to make a special calendar app, but I am afraid it's not possible to safely schedule an alert for an event.
For example: I set up an alert for an event which starts in 3 months. I want to get notified 2 days before the event starts. In iOS 4 there is multitasking, so my app could run in the background all the time.
But now lets imagine it's a hardcore iPhone user who plays huge memory-intensive games all the time. At some point, iOS might kill my background app. Or the user might restart the device and forget to launch my app. So it could happen that the alert never happens. Bad thing.
Is there a safe way to ensure that an scheduled alert is thrown at the user, just like it is the case with the built in alarm clock app or the calendar app?
I'm going to bring back the EventKit notification - use event kit to schedule a calendar entry with an alert, and embed in there a URL that will open your app.
You could also use local notifications but this way the user will be able to see the upcoming event when reviewing the calendar, and even modify slightly if need be. They can't mess with a local notification once it's in place...
You want to use UILocalNotification for this.
EventKit will make it pop up in the user's calendar, maybe not what you want here.
BTW: Multitasking is really more "fast switching" than backgrounding in iOS... you won't be able to run arbitrary code in background, and you should expect to be killed anytime.