Swift

Control Rclone from Swift using the remote control HTTP API with URLSession.

Swift calls Rclone's remote control API with URLSession, which is part of Foundation, so no third-party dependency is needed on Apple platforms.

Usage

The sample creates a data task for the root of the API. The completion handler prints the error if the request failed; otherwise it decodes the body as UTF-8 and prints it. RunLoop.main.run() keeps the program running so the asynchronous task can complete.

import Foundation

let url = URL(string: "http://localhost:5572/")!
let task = URLSession.shared.dataTask(with: url) { data, response, error in
    if let error = error {
        print("Error: \(error)")
        return
    }
    if let data = data, let body = String(data: data, encoding: .utf8) {
        print(body)
    }
}
task.resume()

// Keep the program running for async task
RunLoop.main.run()

Using async/await (iOS 15+, macOS 12+):

import Foundation

Task {
    let url = URL(string: "http://localhost:5572/")!
    let (data, _) = try await URLSession.shared.data(from: url)
    if let body = String(data: data, encoding: .utf8) {
        print(body)
    }
}

How is this guide?

On this page