iOS image and video upload

Cloudinary provides support for uploading media directly from your mobile application to your Cloudinary account, without going through your servers first. This method allows for faster uploading and a better user experience. It also reduces load on your servers and reduces the complexity of your applications.

You can also take advantage of upload presets for predefining upload parameters. For details on all the available upload parameters, see the Upload Images and Upload Videos documentation and the Upload method of the Upload API Reference.

For security reasons, mobile applications shouldn't contain your Cloudinary account credentials, and so they cannot freely upload resources to your account. You can use a signed upload, but that requires generating an authentication signature on your backend. In most cases, you will probably use unsigned uploads that generally provide all the functionality you need for your mobile application, while restricting upload options that require more security.

Upload method

The upload request is managed by the upload method, which accepts the file to upload as its only parameter. The file can be specified as either a url or data object.

cloudinary.createUploader().upload(url: fileUrl)
// or
cloudinary.createUploader().upload(data: data)

The OS attempts to initiate the upload immediately but the upload may be rescheduled depending on the system's current network, memory and cpu needs. The upload results are dispatched asynchronously and callbacks can be defined per request.

The following simple example uploads an image called imageFile.jpg using the default settings:

cloudinary.createUploader().upload(url: "imageFile.jpg")

Signed upload

Signed uploads require you to generate an authentication signature on your backend, and as such, iOS signed upload with backend support should be implemented in conjunction with one of Cloudinary's backend frameworks (Java, .NET, etc). The various backend frameworks implement helpers to be used in conjunction with iOS, as well as automatically generate the authentication signature for the upload.

Unsigned upload

Unsigned upload is an option for performing upload without the need to generate a signature on your backend. Unsigned upload options are controlled by an upload preset: to use this feature, you first need to enable unsigned uploading for your Cloudinary account from the Upload Settings page.

An upload preset is used to define which upload options will be applied to images that are uploaded unsigned with that preset specified. You can edit the preset at any point in time (or create additional upload presets), to define the parameters that will be used for all images that are uploaded unsigned from your mobile application.

Upload options

You can pass an instance of CLDUploadRequestParams, with any extra parameters you'd want to pass, as part of the upload request. For example, you can specify your own public ID instead of a randomly generated one.

The following simple example uploads an image called imageFile.jpg using an upload preset called sample_preset:

let params = CLDUploadRequestParams()
params.setUploadPreset("sample_preset")
let request = cloudinary.createUploader().upload(file: fileUrl, params: params)

The result of the upload API call is a CLDUploadResult object that provides information about the uploaded image, including the assigned public ID of the image and its URL.

If you want to include more than one upload parameter in the request you can chain them together. For example, to upload an image called dog.jpg, set the public_id to MyDog, and add the tag animal:

let params = CLDUploadRequestParams()
    .setUploadPreset("sample_preset").setPublicId("MyDog").setTags("animal")
let request = cloudinary.createUploader().upload(file: fileUrl, params: params)

Note: For security reasons, only this restricted set of upload options can be specified directly when performing unsigned upload calls. All other upload parameters can only be specified within the upload preset.

Callbacks

You can track upload progress by passing a progress closure that is called periodically during the data transfer, and a completionHandler closure to be called once the upload request has finished. For example:

let request = cloudinary.createUploader().upload(
  file: fileUrl, progress: { (bytes, totalBytes, totalBytesExpected) in
    // Handle progress
    }) { (response, error) in
        // Handle response
}