Uploadcare 0.2.0

Uploadcare 0.2.0

TestsTested
LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Sep 2020

Maintained by Uploadcare, Inc.



Swift API client for Uploadcare

license swift Build Status

Uploadcare Swift API client for iOS, iPadOS, tvOS, macOS, and Linux handles uploads and further operations with files by wrapping Uploadcare Upload and REST APIs.

Check out out Demo App.

Installation

Swift Package Manager

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

dependencies: [
    .package(url: "https://github.com/uploadcare/uploadcare-swift.git", from: "0.1.0")
]

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

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

Or you can add it in Xcode: 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" "0.1.0"

To use 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:

let uploadcare = Uploadcare(withPublicKey: "YOUR_PUBLIC_KEY")
// Secret key is optional. Initialization with secret key:
let uploadcare = Uploadcare(withPublicKey: "YOUR_PUBLIC_KEY", secretKey: "YOUR_SECRET_KEY")

Using Upload API

Check full Upload API documentation for all available methods.

Example of direct uploads:

guard let url = URL(string: "https://source.unsplash.com/random") else { return }
let data = try? Data(contentsOf: url) else { return }

// You can create UploadedFile object to operate with it
let fileForUploading1 = uploadcare.uploadAPI.file(fromData: data)
let fileForUploading2 = uploadcare.uploadAPI.file(withContentsOf: url)

// Handle error or result
fileForUploading1.upload(withName: "random_file_name.jpg", store: .store) { (result, error) in
}

// Completion block is optional
fileForUploading2?.upload(withName: "my_file.jpg", store: .store)

// Or you can just upload data and provide filename
let task = uploadcare.uploadAPI.upload(files: ["random_file_name.jpg": data], store: .store, expire: nil, { (progress) in
    print("upload progress: \(progress * 100)%")
}) { (resultDictionary, error) in
    if let error = error {
        print(error)
        return
    }

    guard let files = result else { return }			
    for file in files {
        print("uploaded file name: \(file.key) | file id: \(file.value)")
    }
}

// You can cancel uploading if needed
task.cancel()

Using REST API

Check full REST API documentation for all available methods.

Example of getting list of files:

// Make a query object
let query = PaginationQuery()
    .stored(true)
    .ordering(.sizeDESC)
    .limit(5)
// Make files list object
let filesList = uploadcare.list()

// Get files list
filesList.get(withQuery: query) { (list, error) in
    if let error = error {
        print(error)
        return
    }
			
    print(list ?? "")
}

Get next page:

// Check if the next page is available
guard filesList.next != nil else { return }
// Get the next page
filesList.nextPage { (list, error) in
    if let error = error {
        print(error)
        return
    }	
    print(list ?? "")
}

Get previous page:

// Check if the previous page is available
guard filesList.previous != nil else { return }
// Get the previous page
filesList.previousPage { (list, error) in
    if let error = error {
        print(error)
        return
    }	
    print(list ?? "")
}

Useful links

Swift Upload API client documentation
Swift REST API client documentation
Uploadcare documentation
Upload API reference
REST API reference
Changelog
Contributing guide
Security policy
Support