How to Pause and Resume Uploading process in iOS application? - ios

I am working with an iOS application in which i have to upload the images on service (Facebook) from my iOS application.When i start uploading i want the functionality to Pause and Resume method while Uploading. I don't know how to do these functionalities? please help me out if you have any information.
Thanks in advance.

use NSURLSessionUploadTask for uploading a file. NSURLSessionUploadTask class is inherited from NSURLSessionDataTask : NSURLSessionTask : NSObject.
And NSURLSessionTask provides Controlling the Task State
– cancel
– resume
– suspend
useful links: <NSURLSessionTask>
NSURLSessionUploadTask
Try to implement this.
If you are using AFNetworking you can also achieve the same by using AFHTTPRequestOperation.

Related

Does NSURLSessionDataTask get paused on background?

I want to know about the behaviour of NSURLSessionDataTask in background.
Does NSURLSessionDataTask get paused right away (as soon as the app enters background)? or does iOS give some time hopefully 30 seconds or so until the response ?
No, You will not continue with NSURLSessionDataTask in background mode with defaultSessionConfiguration !
If you want to continue execution in background then you need to configure session with backgroundSessionConfiguration.
When you are using backgroundSessionConfiguration you can't send data directly to server, you have to give file url and from that url you have to send bytes or chunks to server, and you have to use uploadTask or downloadTask with backgroundSessionConfiguration!!
If you wants little time after entering in background then you can use UIBackgroundTaskIdentifier. These are very huge concepts to explain every thing here, It's better that you read documentations from apple or some tutorial for that!!! You should refer Apple Documentation for Background Execution and Apple documentation for NSURLSession !

resuming upload of file after app crash using nsurlsession

I want to upload a file to a server using NSURLSession.
Cases Are:
1. It should resume uploading a file to the server from where it stopped because of app crash.
2. It should handle background upload as well.
Try AFNetworking Library to upload image asynchronously.You can find a brief example in this thread.
You should use background NSURLSession. If your app crashed or user left the app while upload was in progress, with a background NSURLSession the upload would continue seamlessly in the background. When the upload is done, your app will be notified of this via the delegate (and if your app wasn't alive at the time the download finished, it will be started in a background mode, at which point you can do whatever cleanup you need).
So create NSURLSessionConfiguration with backgroundSessionConfigurationWithIdentifier, and then instantiate a NSURLSession with that configuration.
There are a few caveats:
You cannot use completion handler pattern. You have to use delegate-based implementation.
You have to implement handleEventsForBackgroundURLSession in the app delegate, capturing the completionHandler it passes you and also instantiate the background session again. Likewise, in your NSURLSession delegate methods, you have to implement URLSessionDidFinishEventsForBackgroundURLSession, which will call the saved completion handler.
For more information, see Background Task Considerations in URL Session Programming Guide, see a section of the same name (but different text) in the NSURLSession class reference, or see the WWDC 2013 What's New in Foundation Networking, where Apple first introduced us to background sessions.

NSURLSessionDownloadTask initiation in backgroundURLSession completionHandler

I have a situation where I need to download multiple files sequentially as each download depends on its previous downloaded file. (I am processing the file in background itself)
I am using NSURLSessionConfiguration backgroundSessionConfiguration.
There is a scenario where NSURLSessionDownloadTask initiates while the application is in background. This crashes the app with Assertion permittedbackgroundduration.
So, my question is, am I doing wrong by initiating the download task in background???
Thanks in advance,
- Satya
You have limited time when on background, so chances are that you're taking too long to process the file.
If you don't call the completion handler fast enough, the system will crash your app.
Or you may have made a mistake and you are not calling the completion handler at all on the scenario where NSURLSessionDownloadTask initiates in background.

AFNetworking continue upload after going to background

I use AFAmazonS3 (which is an extention of AFNetworking)
It has AFHTTPRequestOperation that is created and added to the operation queue
[self.operationQueue addOperation:requestOperation];
The thing is that when the app goes to background it stops uploading and doesn't resume when it goes back.
How can I acheive it?
I saw some solutions but it was for the old version of AFNetworking
AFNetworking supports Background operations.This post is mentioning setShouldExecuteAsBackgroundTaskWithExpirationHandler: and explaining how to use it https://stackoverflow.com/a/7881866/3033056 .
This method is in AFURLConnectionOperation and AFHTTPRequestOperation inherits from AFURLConnectionOperation

AFNetworking 2 and background tasks

I have a question about AFNetworking 2 and background downloads/uploads thanks to the new iOS7 NSURLSession background requests
Is this automatically handled by my AFHTTPRequestOperationManager ? Does it automatically set my requests'session to background mode?
I saw that the AFURLSessionManager Has a setDidFinishEventsForBackgroundURLSessionBlock Method but I wonder if everything is automatic?
If my app is killed or suspended, will requests keep on going? How can I get a callback when my app is relaunched?
Thanks a lot for your help!
AFHTTPRequestOperationManager uses the old NSURLConnection so that doesn't facilitate background downloading.
AFURLSessionManager uses NSURLSession under the hood so that does. I think you still need to configure the NSURLSession appropriately.
"The NSURLSession class supports background transfers while your app is suspended. Background transfers are provided only by sessions created using a background session configuration object (as returned by a call to backgroundSessionConfiguration:)."
Suggested reading:URL Loading System

Resources