Upload images on background mode - ios

I have web service which i want to use to upload image to the server, web service proxy classes generated by wsdl2objc it uses NSOperation to perform soap calls. Suppose during the upload process i press the home button and application enters the background mode, what will be in that case? will the upload process terminate? or process will complete anyway.

By default, the OS will freeze your app in the background. When that happens, the remote server will probably close the connection after a while because your app doesn't respond.
You can avoid that by wrapping your upload code in a background task (with the methods beginBackgroundTaskWithExpirationHandler: and endBackgroundTask:), in which case the OS will let your app run in the background for another 10 minutes to finish its work.

Related

NSUrlSession suspend and resume issue on device lock

I am uploading file to server by using multipart form data with NSURLSession. When the application goes in background I want to suspend the request and resume when application comes in foreground again. So I simply do [session suspend] and [session resume]. This is working fine when the app goes in background only. But if the device gets locked, when going back in foreground and try to resume, I get a network connection lost error. I understand that when device is locked, all open sockets are closed and therefore the issue, but is there some way to make this work without the need of starting the upload from beginning?
You should switch your foreground session to a background session before the app goes to background, and then there is no need to suspend it. Your file will be uploaded by OS while in background (eventually).
Unfortunately, according to the documentation, you need to use a file to perform your background upload.
From "Background Transfer Considerations":
Only upload tasks from a file are supported (uploading from data objects or a stream will fail after the program exits).
(In addition to that, there is no guarantee when or why your app will be terminated. Trying to avoid device locking alone won't be sufficient; there are a myriad other ways your app may be terminated.)

How to run an swift app in background when close app?

I want some information stored in the database server if disconnected the Internet stored database sqlite when Internet connection to send data to the server (even when the app is closed) What is your suggestion for the job
This cannot be done, your application will work for just a short time in background after you terminate the application.
Read more about the Background Execution on Apples Developer site.
iOS app can't run on background. You can handle changes of connection (wifi , 4g, none) when app is on foreground. Or you can add background task which gives you additional time to save data into database etc.
Method is called beginBackgroundTaskWithName:expirationHandler: in applicationDidEnterBackground. More documentations is here Executing Finite-Length Tasks : https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

Timeout when using NSUrlSession with call started in foreground but app moved to background

If a basic call to post some data to a server using NSURLSession (in this case using Alamofire) a call is started with the app in foreground, while the call is in progress the app is move to the background and is suspended. When the app comes back to the foreground, what happens to the call that was in progress? Does it timeout (App is using default 60 seconds ephemeralSessionConfiguration) or will it receive some other error? If the timeout has not yet been reached is the call still waiting or has the OS terminated it? I have the default background mode and have no requested no background time. Trying to debug an issue that happened in the field.
Your network requests are suspended when your app is suspended. So basically the answer is that it depends on whether the server gives up on you while your app is in the background, and on whether the socket in the kernel gives up on your app while it is suspended.
As a rule, if you need to move data while your app is in the background, you should be using a background session and a download or upload task. That way, the actual data transfer happens in a separate process (that doesn't get suspended) and your app will get the data.
With that said, using a background session is fundamentally in conflict with using an ephemeral configuration, because it involves storing data on disk. So if you really need an ephemeral configuration for some reason, your app's requests are likely to just fail a few seconds after the user hits the home button, and there's not much you can do about it as long as you're making requests ephemerally.

iOS background uploading of images

I have made an application which uploads a bunch of photos to a web server. It does so by repeated html calls, using multiple AFNetworking's AFHTPRequestOperation inside an operation queue. Right now, of the user exits the application, the queue stops. However, I want to continue the uploading queue until it's done, and then let the application go to sleep like it normally does.
I know that iOS provides a background expiration handler using "beginBackgroundTaskWithExpirationHandler" . I also know that AFURLConnectionOperation which is superclass of every HTTP operation class in AFNetworking can use that using
- (void)setShouldExecuteAsBackgroundTaskWithExpirationHandler:(void (^)(void))handler
But is there any way of making this work with an operation queue? Will setting the expiration handler of each operation do the job properly if I want to upload, let's say 10 photos?
I would appreciate any comments on background tasks with AFNetworking, or if anyone has experienced the same problem as me.
When an app goes to background, the OS will decide whether or not to completely stop your app or give it some time to complete what it's doing. In case it wants your app to stop, the expiration handler is called. If that's happening you should suspend all your tasks as fast as possible and prepare for a complete kill of the app, because that is what will happen a very short time after (5 seconds max). Take a look here.

iOS processes while in background

Is it possible to interface with a webservice (or, for what matter, do any scheduled local or remote activity) while the app is in background?
I know that you can receive remote pushes or schedule local alerts, but I wonder if I can periodically send my GPS position to a webservice even if the app is not in foreground.
I have been thinking about this myself in an attempt to impress my boss with an iPhone/iPad App that accesses our Web Service.
If the Web Service takes a non-trivial amount of time to process a request then there is absolutely no guarantee that the App won't be interrupted and stopped, therefore making it useless for any business-level tool. The only Apps that are allowed to run for extended periods in the background are a select set that use certain frameworks (music players, etc.).
I concluded that the only way of doing it is to introduce a middle tier that performs and waits for the response from the actual Web Service and provides an interface that the iOS App can poll or be pushed to in order to allow it to sleep/die whenever iOS thinks it should.
You can make sporadic network calls while you are running in the background if you are a location-based app with proper permissions. You need to make sure you are running a background task properly. FYI, there are a number of applications in the app store that do this.

Resources