This repository contains the Swift SDK for Aspose.BarCode Cloud.
- Swift Package Manager
- iOS 13.0 or later
- macOS 10.15 or later
Add the package to your SwiftPM dependencies after the repository is published:
.package(url: "https://github.com/aspose-barcode-cloud/Aspose.BarCode-Cloud-SDK-for-Swift.git", from: "26.6.0")Releases use the BarCode SDK tag style, for example v26.6.0. SwiftPM version requirements still use the semantic version value without the v prefix.
Then import the module:
import AsposeBarcodeCloudThe first generated API surface includes GenerateAPI, RecognizeAPI, ScanAPI, and the corresponding models.
let client = AsposeBarcodeCloudClient(
clientId: "your-client-id",
clientSecret: "your-client-secret"
)The OAuth access token is fetched lazily on the first API call. To warm it up at app start, await the explicit method:
_ = try await client.authorize()If you already have an access token, configure the SDK directly:
let client = AsposeBarcodeCloudClient(accessToken: "your-access-token")AsposeBarcodeCloudClient owns a per-instance apiConfiguration and sets x-aspose-client and x-aspose-client-version headers on it. The bundled BarcodeAuthInterceptor injects the Authorization header on each authenticated request. Pass client.apiConfiguration to each API call so the headers are applied.
GenerateAPI.generate(
barcodeType: .qr,
data: "Aspose.BarCode Cloud",
imageFormat: .png,
apiConfiguration: client.apiConfiguration
) { data, error in
if let error = error {
print(error)
return
}
print("Generated bytes:", data?.count ?? 0)
}Use scanBase64 when the SDK should detect barcode types automatically, or recognizeBase64 when you know which barcode types to look for.
let imageBase64 = generatedPngData.base64EncodedString()
ScanAPI.scanBase64(
scanBase64Request: ScanBase64Request(fileBase64: imageBase64),
apiConfiguration: client.apiConfiguration
) { response, error in
if let error = error {
print(error)
return
}
print(response?.barcodes?.first?.barcodeValue ?? "")
}
let request = RecognizeBase64Request(
barcodeTypes: [.qr],
fileBase64: imageBase64
)
RecognizeAPI.recognizeBase64(
recognizeBase64Request: request,
apiConfiguration: client.apiConfiguration
) { response, error in
if let error = error {
print(error)
return
}
print(response?.barcodes?.first?.barcodeValue ?? "")
}Full runnable sample at Examples/GenerateAndScan/main.swift:
import AsposeBarcodeCloud
import Foundation
private enum ExampleConfiguration {
private static let defaultConfigPath = "Tests/configuration.json"
static func load() throws -> AsposeBarcodeCloudConfiguration? {
if let configuration = loadFromEnvironment(ProcessInfo.processInfo.environment) {
return configuration
}
return try loadFromFile(defaultConfigPath)
}
private static func loadFromEnvironment(_ environment: [String: String]) -> AsposeBarcodeCloudConfiguration? {
let payload = Payload(
accessToken: firstValue(in: environment, names: [
"TEST_CONFIGURATION_ACCESS_TOKEN",
]),
clientId: firstValue(in: environment, names: [
"TEST_CONFIGURATION_CLIENT_ID",
"ASPOSE_CLIENT_ID",
]),
clientSecret: firstValue(in: environment, names: [
"TEST_CONFIGURATION_CLIENT_SECRET",
"ASPOSE_CLIENT_SECRET",
]),
host: firstValue(in: environment, names: [
"TEST_CONFIGURATION_HOST",
"TEST_CONFIGURATION_BASE_URL",
]),
tokenURL: firstValue(in: environment, names: [
"TEST_CONFIGURATION_TOKEN_URL",
])
)
return payload.makeConfiguration()
}
private static func loadFromFile(_ path: String) throws -> AsposeBarcodeCloudConfiguration? {
guard FileManager.default.fileExists(atPath: path),
let data = FileManager.default.contents(atPath: path)
else {
return nil
}
let payload = try JSONDecoder().decode(Payload.self, from: data)
return payload.makeConfiguration()
}
private static func firstValue(in environment: [String: String], names: [String]) -> String? {
for name in names {
if let value = environment[name], !value.isEmpty {
return value
}
}
return nil
}
private struct Payload: Decodable {
let accessToken: String?
let clientId: String?
let clientSecret: String?
let host: String?
let tokenURL: String?
enum CodingKeys: String, CodingKey {
case accessToken
case clientId
case clientSecret
case host
case baseUrl
case tokenURL
case tokenUrl
}
init(
accessToken: String? = nil,
clientId: String? = nil,
clientSecret: String? = nil,
host: String? = nil,
tokenURL: String? = nil
) {
self.accessToken = accessToken
self.clientId = clientId
self.clientSecret = clientSecret
self.host = host
self.tokenURL = tokenURL
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
accessToken = try container.decodeIfPresent(String.self, forKey: .accessToken)
clientId = try container.decodeIfPresent(String.self, forKey: .clientId)
clientSecret = try container.decodeIfPresent(String.self, forKey: .clientSecret)
host = try container.decodeIfPresent(String.self, forKey: .host)
?? container.decodeIfPresent(String.self, forKey: .baseUrl)
tokenURL = try container.decodeIfPresent(String.self, forKey: .tokenURL)
?? container.decodeIfPresent(String.self, forKey: .tokenUrl)
}
func makeConfiguration() -> AsposeBarcodeCloudConfiguration? {
if let accessToken, !accessToken.isEmpty {
return AsposeBarcodeCloudConfiguration(
accessToken: accessToken,
host: host ?? AsposeBarcodeCloudConfiguration.defaultHost,
tokenURL: tokenURL ?? AsposeBarcodeCloudConfiguration.defaultTokenURL
)
}
guard let clientId, !clientId.isEmpty,
let clientSecret, !clientSecret.isEmpty
else {
return nil
}
return AsposeBarcodeCloudConfiguration(
clientId: clientId,
clientSecret: clientSecret,
host: host ?? AsposeBarcodeCloudConfiguration.defaultHost,
tokenURL: tokenURL ?? AsposeBarcodeCloudConfiguration.defaultTokenURL
)
}
}
}
guard let configuration = try ExampleConfiguration.load() else {
print("Set TEST_CONFIGURATION_ACCESS_TOKEN or TEST_CONFIGURATION_CLIENT_ID/TEST_CONFIGURATION_CLIENT_SECRET.")
exit(1)
}
let client = AsposeBarcodeCloudClient(configuration: configuration)
let barcodeValue = CommandLine.arguments.dropFirst().first ?? "Aspose.BarCode Cloud Swift example"
print("Generating QR barcode...")
let imageData = try await GenerateAPI.generate(
barcodeType: .qr,
data: barcodeValue,
barcodeImageParams: BarcodeImageParams(imageFormat: .png),
qrParams: QrParams(
qrEncodeMode: .auto,
qrErrorLevel: .levelM,
qrVersion: .auto,
qrAspectRatio: 0.75
),
apiConfiguration: client.apiConfiguration
)
try imageData.write(to: URL(fileURLWithPath: "QR.png"))
print("Generated image saved to QR.png")
print("Scanning generated barcode...")
let response = try await ScanAPI.scanBase64(
scanBase64Request: ScanBase64Request(fileBase64: imageData.base64EncodedString()),
apiConfiguration: client.apiConfiguration
)
if let first = response.barcodes?.first {
print("Recognized type: \(first.type ?? "")")
print("Recognized value: \(first.barcodeValue ?? "")")
} else {
print("No barcodes recognized.")
}The generated source is maintained from the aspose-barcode-cloud-codegen repository:
cd ../aspose-barcode-cloud-codegen
make swiftRun the default local checks from this package directory:
make testRun live integration smoke tests with Aspose Cloud credentials:
cp Tests/configuration.example.json Tests/configuration.json
# Fill clientId and clientSecret.
make integration-testAlternatively, keep credentials in local environment variables:
cp .env.integration.example .env.integration
# Fill TEST_CONFIGURATION_CLIENT_ID and TEST_CONFIGURATION_CLIENT_SECRET, or TEST_CONFIGURATION_ACCESS_TOKEN.
make integration-testRun the sample program:
make exampleThe sample writes QR.png locally.