I want to create a simple app, where the user can take a photo. The critical thing is that I need to guarantee that the photo gets uploaded to our server as soon as possible. This must be true, even if the photo was taken, while there was no internet connection, or even if the battery runs dry in the middle of the uploading, or any other scenario you can think of.
The easiest way to solve this is by using a progress bar, but I really do not want my users to wait unnecessarily.
It is not good enough that the photo will get uploaded the next time the app is started, as I need the photo as fast as possible.
My question is, whether it is possible to guarantee that the photo gets uploaded in the background?
This question is somewhat similar, but I do not have anything to use the location for, so the app would probably get rejected my the App Store.
In a nutshell: you cannot guarantee anything. You can only try and hope it gets uploaded, there are some things you can do to maximise your chance but if there’s no connectivity for a long period there’s nothing you can do given the constraints you mention.
You can only execute the upload while the app is in the foreground.
Unless you have a background type – as you’ve guessed if you’re pretending to have location, or one of the other types, and you don’t, then you’ll be rejected.
So the only other option you have available is to make use of beginBackgroundTaskWithExpirationHandler.
This will give you about 10 minutes to execute in the background once the user has tasked away from your app (but if they terminate the app there’s nothing you can do).
If there is no connectivity while the app is in the foreground + the extra 10 minutes once its moved to the background there is nothing you can do other than try again next time the app starts.
Use the Reachability class to see if there is reachability to the url, if so try and upload it, if not register for notification of change in the reachability status and as soon as reachability appears try to upload it again.
If you have reachability try and upload it, if there is a network failure just keep retrying until the 10 minutes is up.
P.S.
How can you possibly expect to guarantee it gets uploaded when the battery runs out?