A simple and intuitive way for interacting with the Lemon Squeezy License API in Swift.
The LemonSqueezyLicense
package provides a simple and intuitive way to integrate Lemon Squeezy's licensing system into your Swift applications. It allows you to perform key operations such as activating, deactivating, and validating license keys.
You can add LemonSqueezyLicense
as a dependency to your project using Swift Package Manager by adding it to the dependencies value of your Package.swift
.
dependencies: [
.package(url: "https://github.com/kevinhermawan/swift-lemon-squeezy-license.git", .upToNextMajor(from: "1.0.0"))
],
targets: [
.target(
/// ...
dependencies: [.product(name: "LemonSqueezyLicense", package: "swift-lemon-squeezy-license")])
]
Alternatively, in Xcode:
- Open your project in Xcode.
- Click on
File
->Swift Packages
->Add Package Dependency...
- Enter the repository URL:
https://github.com/kevinhermawan/swift-lemon-squeezy-license.git
- Choose the version you want to add. You probably want to add the latest version.
- Click
Add Package
.
You can find the documentation here: https://kevinhermawan.github.io/swift-lemon-squeezy-license/documentation/lemonsqueezylicense
To start using the LemonSqueezyLicense
package, first import it and create an instance of the LemonSqueezyLicense
struct:
import LemonSqueezyLicense
let license = LemonSqueezyLicense()
To activate a license key for a new instance of your application:
do {
let response = try await license.activate(key: "your-license-key", instanceName: "User's Mac")
if response.activated {
print("License activated successfully!")
print("Instance ID: \(response.instance?.id ?? "N/A")")
} else {
print("License activation failed: \(response.licenseKey?.status ?? "Unknown status")")
}
} catch {
print("An error occurred: \(error)")
}
To validate an existing license key:
do {
let response = try await license.validate(key: "your-license-key", instanceId: "instance-id")
if response.valid {
print("License is valid!")
} else {
print("License is not valid: \(response.licenseKey?.status ?? "Unknown status")")
}
} catch {
print("An error occurred: \(error)")
}
To deactivate a license for a specific instance:
do {
let response = try await license.deactivate(key: "your-license-key", instanceId: "instance-id")
if response.deactivated {
print("License deactivated successfully!")
} else {
print("License deactivation failed: \(response.licenseKey?.status ?? "Unknown status")")
}
} catch {
print("An error occurred: \(error)")
}
The package uses LemonSqueezyLicenseError
to represent specific errors that may occur during API interactions. You can catch and handle these errors as follows:
do {
let response = try await license.activate(key: "your-license-key", instanceName: "User's Mac")
// Handle successful response
} catch let error as LemonSqueezyLicenseError {
switch error {
case .badServerResponse:
print("Received an invalid response from the server")
case .serverError(let statusCode, let errorMessage):
print("Server error (status \(statusCode)): \(errorMessage ?? "No error message provided")")
}
} catch {
print("An unexpected error occurred: \(error)")
}
If you find LemonSqueezyLicense
helpful and would like to support its development, consider making a donation. Your contribution helps maintain the project and develop new features.
Your support is greatly appreciated!
Contributions are welcome! Please open an issue or submit a pull request if you have any suggestions or improvements.
This repository is available under the Apache License 2.0.