iPhone App Architecture - iphone

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.

Related

Is it possible to run an application from my application when it's running background state?

Is it possible that my iphone application will run in background state and after some time interval it will start another program or application from my iphone.
Suppose, I want to start the camera preview after 10 minute later that will be handled from my iphone. So, my application will run in background state and it will start the camera view after 10 minutes.
Is it possible ???
The only way to start other applications is to use the url schemes exposed by the other app. If it does not expose such scheme, you won't be able to start it.
What about scheduling such thing when you're in the background, you can register for timed local notifications that will show a popup to the user when the time has been elapsed. If the user accepts the popup, your application will get focus and CPU so you can launch also other apps.
There is another option, to get some seconds of CPU in every 10 minutes. It is called VoIP services and you can register for it in the project settings, then it'll call a callback in your app delegate when the OS decides to grant you some CPU.
Study "local notifications" and "url schemes", these are the technologies you need.
Specifically on whether your app can do anything while in the background state, recommend watching the 2010 WWDC video "Session 105 - Adopting Multitasking on iPhone OS, Part 1" : https://developer.apple.com/itunes/?destination=adc.apple.com.4092349126.04109539109.4144345587?i=1907522673
TL;DR: you can only finish up tasks upon entering the background. MrTJ is right about using a timed local notification, and you can also investigate Apple Push Notifications too, if a bit more work and outside the scope of your original ask.

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.

LocalPushNotification in iphone

When iPhone application go to applicationWillResignActive mode, I need to download the data from specific site continuously(Using Timer) and if any data receive from that site should push the UILocalNotification. But if App has enter into Background, Downloading functions is not calling. Exactly i want as Mail app.
You can't do that unless you're a VoIP app or a GPS app.
For other types of app, the closest you can do is request the 10 minute long task completion mode. That'll give you ten minutes of running whatever you want in the background, after which the system kills your app.

how many days can a app live in background

I am actually doing some daily checks of the app assuming the app will be closed and opened daily. It works fine for 3.1 devices. My question is how many days can a app live in background on iPhone 4.0 OS.
If it lives for a long time what are the best ways to make daily schedules to contact my backend server ?
Backgrounding in iOS4 is more restrict than that. You can schedule 'task completion' but that can only run for 10 minutes. You could possibly do the 'location' background service but that would use quite a bit of power and I'm not sure if it would let you contact your server like you are talking about.
I suppose a more direct answer to your question is that it depends. When you are in the background, you need to be prepared to have you app closed at anytime. When the device runs low on memory, it will close any app that hasn't be run in a while.
Apple's Doc on Background Tasks
Why do you say it works fine for 3.1 devices? If you want the app to update when opened from the springboard like it does in 3.1 then you want to use applicationWillEnterForeground: in your app delegate to call the right code.
If you must update periodically in the background then you can use setKeepAliveTimeout:handler: and beginBackgroundTaskWithExpirationHandler, but I don't know what the app store reviewers will think about a non-VOIP app using using it.

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