I am building an app for iOS 7 that allows the user to select pictures and upload these to a server. In a perfect world the user would choose the pictures, press upload and be able to close the app. I looked in to NSURLSession to establish this but it seems to only take a file. Is there any way i can send my NSData like in a NSURLRequest? Also, when not connected to the internet, is there any way i can make the app poll for an internet connection in the background and make it send the pictures when connection is established? I don't think was possible using earlier versions of iOS but iOS 7 seems to have some new options regarding background tasks. Thanks in advance for any help!
2 Answers
A couple of thoughts:
You are correct that background uploads must use a file. So just save the
NSData
to a file (e.g. withwriteToFile
method), and then use that file path.Regarding checking for Internet connection, the background
NSURLSession
takes care of that for you, so, no, you don't have to do that.Regarding background uploads in earlier iOS versions, you could initiate the upload, but explicitly request a little more time to complete this finite-length task while the app runs in the background with
UIBackgroundTaskIdentifier
. See Executing a Finite-Length Task in the Background discussion in the App States and Multitasking section of the iOS App Programming Guide.This isn't quite as robust as the new background
NSURLSession
functionality (which is more clever about applying discretionary logic so your app doesn't significantly adversely affect foreground apps, controlling whether it's permissible to do the upload over cell connection, allowing longer-length requests, working even if your app was terminated (for example, due to memory pressure), etc.). ButUIBackgroundTaskIdentifier
is a possible solution for iOS versions prior to 7 where you want to give an upload request a chance to complete even though the user has left your app.
-
Thanks for clarifying! Writing to a file might proof difficult, because I am using a framework that puts the app in a secure container. The framework probably does not allow me to put files outside the secure container or NSURLSession to take a file from within. Good news on the second point, if I do manage to create a file, NSURLSession will be picked up and delayed until an internet connection is available. Can I force upload over cell connection, or is iOS deciding for me? – FreekHelm Feb 04 '14 at 08:09
-
@FreekHelm Sorry to hear about the constraints of your framework. If you are indeed correct, then you might not be able to use background session. As an aside, if security is that important, you might consider using ephemeral `NSURLSessionConfiguration` instead of standard. On that second point, it's not good news for you, because that only applies to background sessions (which we've concluded you cannot use). Using standard sessions, you have to check availability yourself via [Reachability](https://developer.apple.com/Library/ios/samplecode/Reachability/Introduction/Intro.html). – Rob Feb 04 '14 at 12:43
-
On the "force over cell connection" question, `NSURLSessionConfiguration` doesn't do that. It has "allow over cell connection", but that's the opposite problem. If you really need "force over cell connection", use Reachability to determine connection type, and then act accordingly. – Rob Feb 04 '14 at 12:48
-
What does "put the app in a secure container" mean? Every `.ipa` is a secure container. And the documents folder, etc. exist outside the "app". – Clay Bridges Feb 10 '14 at 19:24
-
@ClayBridges I assumed his requirements were such that he couldn't store anything in persistent storage (e.g. accomplishing business requirements similar to those achieved by `ephemeralSessionConfiguration`, ensuring that if the device is ever compromised that no confidential information would be lingering about). Clearly, if he could save the file to the Documents folder or wherever, then using a `NSURLSessionUploadTask` would work fine. But you're right that maybe I shouldn't have taken the "outside the secure container" comment for granted. – Rob Feb 10 '14 at 19:46
-
@FreekHelm Can you clarify what this "framework" is, which allegedly doesn't allow you to save files "outside the secure container"? Clearly, while you can't save to the app bundle, you generally save files within the app sandbox (e.g. the app's Documents folder) without incident. What is this "secure framework" to which you refer? – Rob Feb 10 '14 at 19:48
-
@Rob: I figured as much, and you made reasonable assumptions; I figured I'd ask anyway. Great answer BTW. +1! – Clay Bridges Feb 10 '14 at 19:54
-
Rob, your assumptions are right, using the GOOD Dynamics SDK. It's similar to the blackberry enterprise server. @ClayBridges, you are right, "outside secure container" is probable not specific enough, but this is the terminology I have come across in the Good community. I have not been able to do more research, but I will definitely follow up on this. – FreekHelm Feb 11 '14 at 07:47
Re: your comment about the "GOOD Dynamics SDK", I looked at it quickly. It does allow SDK-based app-to-app document sharing. I don't know if that means it writes a single encrypted on-disk file in the process, or if it uses an encrypted folder to store everything. If you had iOS access to that file, and a way to decrypt it on the server, then you'd have a chance of using the file-based background upload magic.

- 11,602
- 10
- 68
- 118