Book Image

iOS and OS X Network Programming Cookbook

By : Jon Hoffman
Book Image

iOS and OS X Network Programming Cookbook

By: Jon Hoffman

Overview of this book

Table of Contents (15 chapters)
iOS and OS X Network Programming Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Uploading a file using MKNetworkKit


In this recipe we will show you how to use MKNetworkKit to upload a file to a server by attaching it as part of a multipart form POST request. Since all of MKNetworkKit's functionality is encapsulated within MKNetworkEngine and MKNetworkOperation, we need to create an engine for our upload.

We will be using the addData:forKey:mimeType:filename: method to upload an image. The MKNetworkOperation class also has an addFile:forKey:mimeType:filename: method that allows us to attach a file directly.

Getting ready

This recipe is compatible with both iOS and OS X. We need to download the framework from https://github.com/MugunthKumar/MKNetworkKit and add it to our project. We also need to add the following four frameworks:

  • ImageIO.framework

  • Security.framework

  • SystemConfiguration.framework

  • CFNetwork.framework

How to do it…

Let's create the FileUploadEngine class.

Creating the FileUploadEngine header file

The FileUploadEngine class is a subclass of MKNetworkEngine. This class...