Uploading Files to Amazon AWS S3 in the Background with AFNetworking 2.0

 

Recently, a client asked me to make a change to an app that uploads image files to an S3 bucket so that the transfer happened in the background. The app was using AFNetworking 1.x and an outdated version of the Amazon AWS libraries. After updating those two libraries, I set out to find the best way to integrate the background data task features of NSURLSession with AFNetworking 2.0. Here’s what I found.

First, you’ll need to update your libraries to the correct versions. AFNetworking 2.0 introduces support for NSURLSession via AFURLSessionManager so you’ll need to be using at least version 2.0. The AWS intregration code in this article depends on version 2.0 of the AWSiOSSDK. Using CocoaPods, your dependencies might look like this:

Next, we’ll need to obtain a signed URL from the AWS SDK with which we can setup a data task. The AWS SDK exposes an API called `AWSS3GetPreSignedURLRequest` for this purpose. It’s built using┬áthe Bolts Framework in order to support creating this URL asynchronously. We’ll use the API like this:

Next we can write a method that takes the signed URL and creates a data task that uploads the file.

Finally, we can combine these two methods:

 

That’s all there is to it. Still having trouble? Feel free to shoot me an email.

Really stuck? Or are you looking for a developer to create an app that has this sort of functionality? I’m available for consulting & freelancing work as well.