> For the complete documentation index, see [llms.txt](https://docs.licensespring.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.licensespring.com/sdks/tutorials/getting-started/swift-objective-c/swift-sdk-configuration-and-usage.md).

# Swift SDK Configuration and Usage

Swift SDK allows implementing licensing using the License and LicenseManager classes.

This article has code samples for both approaches. In other articles, we will demonstrate SDK usage only with LicenseManager and License. LicenseHandler class has methods corresponding to License/LicenseManager functionality.

### Configuration

To initialize the SDK, you must fill in your application name and version, LicenseSpring API key, shared key and product code.

Both the API key and shared key can be found on your LicenseSpring account under Settings->Keys:

![API and Shared Key Location](/files/3697e4c2be4ab81d3d98ae325c8d155566af9ad2)

Your product code is located in your product list under Configure Products:

![Product CoProduct Code Location Location](/files/319b735e44c5cb40ed231a69885df8a408b06bad)

{% hint style="warning" %}
The product code field is case-sensitive.
{% endhint %}

When you have the necessary values, create the Configuration.

{% tabs %}
{% tab title="LicenseManager" %}
{% code title="Configuration example" %}

```swift
let configuration = Configuration(
    apiKey: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    sharedKey: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    productCode: "XX"
)
configuration.appName = "My LicenseSpring Application"
configuration.appVersion = "1.0.0"
```

{% endcode %}
{% endtab %}
{% endtabs %}

Configuration has been extended with a nullable extendedOptions parameter, which at this time can be used for guarding your offline activations. Example:

{% tabs %}
{% tab title="LicenseManager" %}
{% code title="Configuration with ExtendedOptions" %}

```swift
let configuration = Configuration(
    apiKey: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    sharedKey: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    productCode: "XX",
    extendedOptions: ExtendedOptions(protectOfflineActivation: true)
)
configuration.appName = "My LicenseSpring Application"
configuration.appVersion = "1.0.0"
```

{% endcode %}
{% endtab %}
{% endtabs %}

Now you can initialize LicenseManager using this configuration object. If apiKey, sharedKey or productCode is empty, an error is thrown.

{% tabs %}
{% tab title="LicenseManager" %}
{% code title="Initialize LicenseManager" %}

```swift
let manager = try LicenseManager(configuration: configuration)
```

{% endcode %}
{% endtab %}
{% endtabs %}

### License Activation

The license keys are located in Licenses section:

![License Key Location](/files/ddac95222da8692b157d804ff921a832395234be)

To implement user-based licensing please refer to: [User-Based Licensing](broken://pages/274a7002013ef0e185613e4e42a57080511fd79e).

To activate a license, create a LicenseID using the given key and call the activation method.

{% tabs %}
{% tab title="LicenseManager" %}
{% code title="Activate license" %}

```swift
let license = try manager.activateLicense(licenseKey: "XXXX-XXXX-XXXX-XXXX")
```

{% endcode %}
{% endtab %}
{% endtabs %}

The process of activating a license refers to binding a device to a license. On the LicenseSpring platform, activating the license increments the total activations counter within the license.

### Local License

The local license is a copy of the license information that is saved on the end user's local computer. Users who have not activated the license on their end before will not have a local license. By default, the local license is stored at:

\~/Library/Application Support///license.ls

License key path and license key name may be customized through Configuration.

See [Local License File](broken://pages/a986371d6914803dc28b6d5303cbc70131c852fe) for more information.

If a device already has a license, and you try to activate another license on that device for the same product, the newly activated license will override the existing license, deleting it from the device. It is the developer's responsibility to check if a license exists on the device before a new activation:

{% tabs %}
{% tab title="LicenseManager" %}
{% code title="Check current license" %}

```swift
if let license = manager.currentLicense { ... }
```

{% endcode %}
{% endtab %}
{% endtabs %}

### License Check

It is recommended to perform a local license check at application start to confirm that the local license file belongs to the current device and has not been transferred.

It is also useful to check whether the local license file has been tampered with and whether the local license is still valid.

{% tabs %}
{% tab title="License" %}
{% code title="Local check" %}

```swift
try license.localCheck()
// throws in case of errors
```

{% endcode %}
{% endtab %}
{% endtabs %}

Full (online) license check performs synchronization of the local license with the data from the LicenseSpring backend:

{% tabs %}
{% tab title="License" %}
{% code title="Full check" %}

```swift
try license.fullCheck()
// throws in case of errors
```

{% endcode %}
{% endtab %}
{% endtabs %}

These methods also update information of InstallationFile. For more information about managing software updates, see [Handling Product Versions](broken://pages/532014277a533f130173d370d9fa017cce8ef1f4).

You can also check for active, valid, enabled, and expired licenses using isActive, isValid, isEnabled, and isExpired methods respectively.

Note that an active license does not necessarily mean a license is valid. A valid license means the license is active, enabled, and not expired. An active license means that the license is bound to a device, but may also be disabled or expired.

### Full Code Sample

Below is a code sample that initializes the SDK, retrieves a local license or activates a new one, and performs the necessary checks.

{% tabs %}
{% tab title="LicenseManager" %}
{% code title="Full example" %}

```swift
do {
    let configuration = Configuration(
        apiKey: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
        sharedKey: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        productCode: "XX"
    )
    configuration.appName = "My LicenseSpring Application"
    configuration.appVersion = "1.0.0"
    
    let manager = try LicenseManager(configuration: configuration)
    let license = try manager.activateLicense(licenseKey: "XXXX-XXXX-XXXX-XXXX")
    try license.localCheck()
    try license.fullCheck()
} catch {
    print("Error occured: \(error)")
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

### License Deactivation

Deactivating a license unbinds the device from the license, and decrements the number of total activations. Depending on the license transfer policy, this usually means that the license is freed up to be activated onto a new device. Deactivating is done using the following method:

{% tabs %}
{% tab title="License" %}
{% code title="Deactivate license" %}

```swift
try license.deactivate()
```

{% endcode %}
{% endtab %}
{% endtabs %}

To remove the local license file after deactivation use LicenseManager.clearLocalStorage method.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.licensespring.com/sdks/tutorials/getting-started/swift-objective-c/swift-sdk-configuration-and-usage.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
