Swift API Client for Upload & Management, and Uploader UI

Swift integration handles uploads and file operations by wrapping Uploadcare Upload API and REST API. This comprehensive API client lets you use most of the Uploadcare features from within your Android app. It also includes a native uploading widget. Builds are available for iOS, iPadOS, tvOS, Linux and macOS.

GitHub →

Check out a swift demo app that we created as a showcase for various usage scenarios and tasks that you can resolve.

Features

Uploading (Upload API):

  • Upload files from a file, URL, and cloud sources (up to 5 TB)
  • Multipart uploading for large files
  • Uploading network to speed uploading jobs (like CDN)

File uploading widget:

  • Upload files from a local disk, camera, and cloud sources
  • Track, pause and continue multipart uploading
  • Background uploading
  • Bulk file uploading

File management (REST API):

  • Get file info and perform various operations (store/delete/copy) with them
  • Work with groups of files
  • Get info about account project
  • Manage webhooks
  • Convert documents
  • Encode and transform videos

Image processing (URL API):

  • Compression
  • Geometry
  • Colors
  • Definition
  • Image and text overlays
  • Rotations
  • Recognition
  • File info
  • Proxy (fetch)

Security features:

  • Secure authentication
  • Secure uploads (signed uploads)
  • Secure delivery (signed URLs)
  • Secure webhooks (signing secret)

Installation

Swift Package Manager

To use a stable version, add a dependency to your Package.swift file:

1dependencies: [
2 .package(url: "https://github.com/uploadcare/uploadcare-swift.git", .branch("master"))
3]

If you want to try the current dev version, change the dependency to:

1dependencies: [
2 .package(url: "https://github.com/uploadcare/uploadcare-swift.git", branch("develop"))
3]

To add from Xcode select File -> Swift Packages -> Add Package Dependency and enter the repository URL:

https://github.com/uploadcare/uploadcare-swift

Or you can add it in Xcode to the packages list using that URL: https://github.com/uploadcare/uploadcare-swift (select master branch).

Carthage

To use a stable version, add a dependency to your Cartfile:

github "uploadcare/uploadcare-swift"

To use the current dev version:

github "uploadcare/uploadcare-swift" "develop"

Cocoapods

To use a stable version, add a dependency to your Podfile:

pod 'Uploadcare', git: 'https://github.com/uploadcare/uploadcare-swift'

To use current dev version:

pod 'Uploadcare', git: 'https://github.com/uploadcare/uploadcare-swift', :branch => 'develop'

Initialization

Create your project in Uploadcare Dashboard and copy its API keys from there.

Upload API requires only a public key, while REST API requires both public and secret keys:

1final class MyClass {
2 private var uploadcare: Uploadcare
3
4 init() {
5 self.uploadcare = Uploadcare(withPublicKey: "YOUR_PUBLIC_KEY")
6
7 // Secret key is optional if you want to use Upload API only
8 // REST API requires both public and secret keys:
9 self.uploadcare = Uploadcare(withPublicKey: "YOUR_PUBLIC_KEY", secretKey: "YOUR_SECRET_KEY")
10 }
11}

You can create more Uploadcare objects if you need to work with multiple projects in your Uploadcare account:

1final class MyClass {
2 private let project1: Uploadcare
3 private let project2: Uploadcare
4
5 init() {
6 // A project to use Upload API only
7 self.project1 = Uploadcare(withPublicKey: "YOUR_PUBLIC_KEY_1")
8
9 // A project to use both REST API and Upload API
10 self.project2 = Uploadcare(withPublicKey: "YOUR_PUBLIC_KEY_2", secretKey: "YOUR_SECRET_KEY_2")
11 }
12}

Keep in mind that since Uploadcare is not a singleton. You should store a strong reference (as an instance variable, for example) to your Uploadcare object or it will get deallocated.

Using Upload API

Check the Upload API documentation to see all available methods.

Each method has an implementation with a Result completion handler and has an alternative async implementation to use with Swift concurrency.

Example of uploads:

1guard let url = URL(string: "https://source.unsplash.com/featured") else { return }
2guard let data = try? Data(contentsOf: url) else { return }
3
4// You can create an UploadedFile object to operate with it
5var fileForUploading1 = uploadcare.file(fromData: data)
6fileForUploading2.metadata = ["myKey": "myValue"]
7try await fileForUploading1.upload(withName: "random_file_name.jpg", store: .store)
8
9// The same method with a completion callback that returns a task that can be paused or cancelled
10
11var fileForUploading2 = uploadcare.file(withContentsOf: url)!
12let file = try await uploadcare.uploadFile(data, withName: "random_file_name.jpg", store: .auto) { progress in
13 print("upload progress: \(progress * 100)%")
14}
15
16// Same method with a completion callback that returns a task that can be paused or cancelled:
17let task = uploadcare.uploadFile(data, withName: "random_file_name.jpg", store: .store, metadata: ["someKey": "someMetaValue"]) { progress in
18 print("upload progress: \(progress * 100)%")
19} _: { result in
20 switch result {
21 case .failure(let error):
22 print(error.detail)
23 case .success(let file):
24 print(file)
25 }
26}
27
28// Cancel uploading if needed
29task.cancel()
30
31// task will confirm UploadTaskable protocol if file size is less than 100 mb, and UploadTaskResumable if file size is >= 100mb
32// You can pause or resume uploading of file with size >= 100mb if needed
33(task as? UploadTaskResumable)?.pause()
34(task as? UploadTaskResumable)?.resume()

It is possible to perform uploads in the background. But implementation is platform-specific. This lib doesn’t provide a default implementation. You can find an example for the iOS in our Demo app. See FilesListStore.swift.

Using REST API

Refer to the REST API documentation for all methods.

Each method has an implementation with a Result completion handler and has an alternative async implementation to use with Swift concurrency.

Example of getting list of files:

1// Make a list of files object
2lazy var filesList = uploadcare.listOfFiles()
3
4func someFilesListMethod() {
5 // Make a query object
6 let query = PaginationQuery()
7 .stored(true)
8 .ordering(.dateTimeUploadedDESC)
9 .limit(5)
10
11 // Get file list
12 let list = try await filesList.get(withQuery: query)
13
14 // Same method with a completion callback
15 filesList.get(withQuery: query) { result in
16 switch result {
17 case .failure(let error):
18 print(error)
19 case .success(let list):
20 print(list)
21 }
22 }
23}

Get next page:

1// Check if the next page is available
2guard filesList.next != nil else { return }
3
4// Async:
5let next = try await filesList.nextPage()
6
7// With a completion callback:
8filesList.nextPage { result in
9 switch result {
10 case .failure(let error):
11 print(error)
12 case .success(let list):
13 print(list)
14 }
15}

Get previous page:

1// Check if the previous page is available
2guard filesList.previous != nil else { return }
3
4// Async:
5let previous = try await filesList.previousPage()
6
7// With a completion callback:
8filesList.previousPage { result in
9 switch result {
10 case .failure(let error):
11 print(error)
12 case .success(let list):
13 print(list)
14 }
15}

Full documentation

Read the full documentation on GitHub.

Demo app

Check the demo app for usage examples:

  • List of files
  • List of groups
  • File info
  • File upload (both direct and multipart, including upload in background)
  • Multiple file upload
  • Pause and continue multipart uploading
  • Project info