Download data in background with iOS4 - iphone

As per the latest update of Kindle V2.5, it has support of "continue downloading books while the app is in the background on iOS 4 devices".
How is it possible to download content in background? As per the iOS multitasking documentation, only audio, voip & location updates are possible in background. And I've also maken sure that NSURLConnection doesn't download new data work while app goes background. Then how's it possible with Kindle app?
Edit: I haven't checked Kindle App in iOS4 multitasking enabled device. So if anyone let me (& community) know what exactly Kindle app does to download, that would be very much helpful.

Yes you can. Go back to the document you mentioned, and check the code at the Completing a Finite Length Task in the Background section. In this way you can run generic tasks in the background, including accessing the network and uploading files.
Edited: I answered a similar question on SO before, check this, iphone - Connecting to server in background

Completing a Finite Length Task in the Background
You can use these methods to do pretty much anything you want, but you've got to finish it all up in <10 minutes. I use it to continue uploading an image after the app has closed.

Related

iphone: backgrounded app sync webservice

I know this question may be a little bit common and over asked but I cannot find any precise information... so :
Is it possible to have some kind of thread running when the app is in background so I can perform basic sync with my webservice ?
App is in background : I mean the user clicked the Home button, or switched to another app
basic sync : photo upload and download with AFNetworking. I know it has method to continue an HTTPRequest while app is in background, but this is not my point.
My goal would be to make some kind of sync manager, reading a list of photos to update created while the user was on the app, and perform those changes.
I know that the manager could be killed by the OS, but since my server uses atomic transfers it is not a problem. I just need a way to relaunch it... Push ?
I think apps like Google Latitude or Mail and those kind of apps uses what I am looking for but I cannot find any relevant details on it. And using iOS5 is not a problem but waiting for iOS6 would not be a solution.
Thank you for your replies !
PS : well I almost forgot. the app is designed for an enterprise program, so maybe rules are different ? I don't think there is any check for in-house deployment so it might lead to new possibilities...
Apple's Mail client has a background daemon which keeps it running but you can't have that with your own applications. Once an app enters a background state, it must halt it's operations. You can request for a little more time when backgrounded to finish off any transfers or writes to disk (see the Executing a Finite-Length Task in the Background section on Apple's Multitasking Guide)
Google Latitude has events generated based on location. This is a special type of backgrounding introduced by Apple for certain types of applications (see Implementing Long-Running Background Tasks section on Apple's Multitasking Guide) but this can't be used for HTTP syncing. It can only be used for audio, location, voip, newstand content, bluetooth and external hardware attachments.
Push doesn't seem like a solution because it only generates an alert. It doesn't trigger any action until the user triggers the opening of the notification.
You'll want to read Tech Note 2277 Networking and Multitasking.
Basically you have a couple of options:
If you can convince Apple that your app is a VoIP app then you can register a VoIP socket and the OS will resume your background app whenever there is activity on that socket.
Your main option though is to register a background task for any outstanding activity that you have to do when your app is put in the background. You typically get 10 minutes to finish up that work.
Mail is a special app with privileges you don't get.
Apps like Latitude typically register themselves for location updates, specifically to be woken up when there are major geo-position changes. Apps that record GPS tracks do similar things.
Found it !
Using Suhail Patel 's link on Apple's Multitasking Guide I added the voip tag to UIBackgroundModes in Info.plist and use setKeepAliveTimeout:handler: method of UIApplication to relaunch it if needed once the app is going to sleep.
I hope this will help a lot of you !
Of course this app won't be allowed to be on the App Store but for in house development this is in my opinion the best way to do so.
Thanks everyone for showing me the right direction !

app runs in background on ios4

I hope to run an app in background on ios4
I know
Apple allows only certain types of apps to run in the background, like navigation and audio and VOIP apps. But even those are limited to only the necessary tasks.
Is it possible I register the app as one kind of VoIP, Audio or GPS apps to keep it run in background?
Welcome any comment
You can't "run" an app in the background; you can only run a task in the background. The tasks are
Continue Playing Audio
Maintain VoIP Connection
Update Location (GPS)
Some Finite Task (such as uploading a file)
I haven't developed for iOS so there might be something I'm missing. Read more at Executing Code in the Background. As of iOS 4, developers don't have the ability to implement true multitasking. Correct me if I'm wrong in any of this.
Not in general, no. You could register a VoIP or GPS session and abuse its callbacks for certain tasks, but I doubt the App Store review process would take kindly to it.
What do you need to do in the background that isn't covered by task completion or the audio/VoIP/GPS background modes? it might be possible to use another paradigm and still get the cake.

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.

Does iOS 4 make “Real Multitasking” available to 3rd party developers?

Ever since the first beta came out I’ve been trying to find out if “real” multitasking is possible — i.e. can you put a program in the background and have it hang on to a network connection indefinitely?
I’m thinking about IM or IRC apps, for example. I’ve compiled an app myself on iOS 4, and without changing a thing it appeared to stay running in the background, but for all I know it was just suspended to memory.
The docs say the best you can do is request up to 10 minutes, but in the developer presentation they showed off Skype sitting in the background and then notifying the user that a call was coming in. Does anyone know for sure how this all works?
It appears the answer is no. The API for Skype is a very special case, called the "voip" mode, and requires special behavior, such as marking the socket in use for VoIP.
You can receive alarm notifications in the background (such as time passed). The amount of time you are in the background running state is severely limited by the OS.
Android's background model is complete and in many ways much nicer.
Apple has a guide named "Supporting Multitasking In Your Applications" which you should be able to locate.
Apple's iOS 4 developer docs outline this all very clearly.
When your app is closed or switched away from, it is almost immediately "suspended", meaning the OS freezes the app's state. When the user switches back to your app, your code keeps running just where it kept off. You don't need to add any code to your app to do this, just compile it against OS 4.
The above is true in most cases. There are two reasons the "suspended" model may not apply:
1) If the device starts to run low on memory, the OS will start terminating suspended apps that haven't been switched to in a while, without warning. This is why it's in your best interest for your app to remember it's state as well, so if your app is terminated, then re-opened, the user doesn't really notice because it still returns to right where they left off.
2) Your app uses one of the "background" APIs. These are for audio playback, VoIP services, or location services. In this case, your app is allowed to continue running in the background but only has access to those APIs. Additionally, your app can designate certain long-running tasks as "background tasks" that need to be completed before the app is suspended or terminated, like uploading pictures to Flickr or rendering a video, etc.
The "background task" method doesn't cover pinging servers indefinitely, as there is a time limit for the task, after which it will be forcibly halted. Apps that need that sort of functionality are expected to implement push notifications, just as before.
That should clear this up. All in all I think it's a pretty elegant solution to multitasking on a mobile device.
iOS 4 applications can either be running or suspended. The operating system will try to keep as many requested applications as possible in memory, while all other applications are suspended.
Applications that run in the background can access features such as navigation, audio, and VOIP (but NOT instant messaging). So it looks like you might be out of luck.
-- PC World Multitasking on Apples iPhone 4
It is possible for apps to request background time. Read the docs. I would say it iOS is "controlled multitasking".
You can create a long running background task, I believe these can include networking features. Just have to set the background task flag on the work block.
https://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
the OS can limit exactly how much time you get though... it will tell you when your time is up giving you a chance to cleanup nicely.
iOS 4 has "real" multitasking to some extend. There are two things to consider:
The UI event loop is single tasking. So only the front app executes on the UI event loop. Not a big deal: detach your main code form the UI event loop (maybe you need to redesign your app).
The OS "may" terminate your app if some criteria are met (e.g. low memory).
Currently one of these criteria is that execution time is limited to 10 minutes (real time not cpu time). However I expect this to change and see better criteria for background app termination (I hope to).
Apart from this you can have timers (event loops) in background.
There is no real multitasking in iOS 4.2 even. because apps will only be allowed to finish the task related to states..for small interval of time and then it will be in suspended state.. If you will set background task for long interval of time then... it will behave unexpectedly like no method will be called when you will try to run the app from anywhere..
You may be interested in this blog post that outlines how "multitasking" works in systems such as iPhone OS 4 and Android.
in fact u can do this, although it's not allowed by Apple. u gotta set up a toolchain in ur mac and use some unofficial SDK...
check http://code.google.com/p/iphone-backgrounder/ for more information
You should use the Push Notifications framework for the feature set you are creating!

iPhone image upload in background

I'm writing an app with an image upload feature. Right now I'm using NSURL POST like: 125306.
When the app is closed as far as I can tell all of the uploads abort and the threads die. Is there
1) a way to make these upload threads persist when the app is no longer in the foreground?
2) an iPhone OS service that will accept requests to queue a job and run it in a mode managed by the OS?
EDIT: This is an old answer from 2009. Current iOS (2016) has background URL tasks. See NSURLSessionUploadTask.
Original answer follows:
You aren't allowed to run in the background on an iPhone (as per Apple's Guidelines). Your app will be rejected from the AppStore if you do.
As has been failry well documented, no background processes are allowed using the SDK.
As noted, you cannot have a background process - a good compromise would be to resize the photo down to a size that could be transferred in a reasonable amount of time (user configurable would be best), and display an activity indicator of some kind so the user would know the upload was in progress.
An even better solution would be the resize, along with a progress indicator giving the percentage currently uploaded - but that involves some custom low-level HTTP code.
After app suspending you only have ~10 mins to do the job.
You should make a background process thread when app is suspended.
And you have no garanties that os will keep app alive. It depends on process complexity.