Skip to content

Networking

URLSession is fully supported on Android via a Java HttpUrlConnection bridge. The bridge solves a common issue with upstream Swift on Android — HTTPS connections fail because the system lacks a trusted root certificate store. The SDK’s implementation delegates to Android’s native HTTP stack, which uses the device’s built-in certificate store, so HTTPS works out of the box with no additional setup.

URLSession APIs work the same as on macOS and iOS. Under the hood, requests are handled by Android’s HttpURLConnection, but this is transparent — standard URLSession usage with redirect handling, cookies, and custom headers works as expected:

let url = URL(string: "https://api.example.com/data")!
URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data, error == nil else { return }
let result = try? JSONDecoder().decode(MyModel.self, from: data)
}.resume()

POST requests work the same way:

var request = URLRequest(url: URL(string: "https://api.example.com/submit")!)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = try JSONEncoder().encode(payload)
URLSession.shared.dataTask(with: request) { data, response, error in
// handle response
}.resume()

The SDK adds async/await versions of URLSession methods that are not available in the Swift open-source Foundation used on Android. These follow the same signatures as on Apple platforms:

// Fetch data
let (data, response) = try await URLSession.shared.data(from: url)
// Download a file to a temporary location
let (localURL, response) = try await URLSession.shared.download(from: url)
// Upload data
let (data, response) = try await URLSession.shared.upload(for: request, from: body)