Skip to main content

Activation

Sample Code

Before using the SDK's scanning features, you must activate the license on the device that will use the SDK. Activation is only required when the SDK is used on a device for the first time, and it requires an online environment.

warning
  • Android: If the device undergoes a factory reset, activation will be required again and the device will be registered as a new device.
  • iOS: If the app is deleted and reinstalled, activation will be required again and the device will be registered as a new device.
Using multiple apps on one Android device

If you use more than one app that includes the EdgeOCR SDK on the same Android device, install EdgeOCR Device Core and select the device ID to use before activation. For details, see Share a Device ID Across Multiple Apps (Android).

1. Initializing NefrockLicenseAPI​

Initialize NefrockLicenseAPI with your license key.

import EdgeOCRSwift

let licenseAPI = try LicenseBuilder()
.withLicenseKey("YOUR_LICENSE_KEY")
.build()
  • Set "YOUR_LICENSE_KEY" to your license key.
  • On Android, pass the app's Context as the first argument.

2. Performing Activation​

Call the activate method to register the device.

do {
let license = try await licenseAPI.activate()
// Activation successful
} catch {
// Activation failed
print(error.localizedDescription)
}

Once activation is performed, the license information is saved to a file, so there is no need to activate again on subsequent app launches.

3. Checking Activation Status​

You can use the isActivated method to check whether the device has already been activated.

do {
let license = try await licenseAPI.isActivated()
// Already activated
} catch {
// Not yet activated
}

This method first checks the local license file. If not found, it queries the server. It will not register the device as a new device.

Checking License Information​

When isActivated or activate succeeds, it returns a License. You can use License to check details such as the license expiration date.

InformationiOS (Swift)Android (Java)
License keylicenseKey: StringgetLicenseKey(): String
Device IDdeviceId: StringgetDeviceId(): String
Start datestartDate: Date?getStartDate(): LocalDate
Expiration dateexpirationDate: Date?getExpirationDate(): LocalDate

If the license has no start date or expiration date, the value is nil on iOS and null on Android.

do {
let license = try await licenseAPI.isActivated()
if let expirationDate = license.expirationDate {
// The date is returned as 00:00 UTC, so format it in UTC
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
formatter.timeZone = TimeZone(identifier: "UTC")
print("Expiration date: \(formatter.string(from: expirationDate))")
} else {
print("Expiration date: none")
}
} catch {
// Not yet activated
}
Confirming a license renewal

When the license file is valid, isActivated and activate return its contents immediately. They then fetch the latest license from the server in the background and update the file. As a result, the first call after the device goes online returns the expiration date from before the update.

To confirm that the expiration date has been extended before returning the device to an offline environment, follow these steps:

  1. Connect the device to the internet.
  2. Quit the app completely, then start it again. The update from the server runs on the first call after the app starts.
  3. Call isActivated.
  4. Wait about 10 to 20 seconds, call isActivated again, and check the expiration date in the returned License.

If the expiration date has not changed, either communication with the server failed or the license has not yet been renewed on the server.

Recommended Pattern

On app launch, first check with isActivated, and only call activate if the device is not yet activated.

func tryActivation(key: String) async -> String? {
let licenseAPI: NefrockLicenseAPI
do {
licenseAPI = try LicenseBuilder().withLicenseKey(key).build()
} catch {
return error.localizedDescription
}

do {
_ = try await licenseAPI.isActivated()
} catch {
// Not yet activated, so perform activation
do {
_ = try await licenseAPI.activate()
} catch {
return error.localizedDescription
}
}

return nil // Success
}

In the sample app, this is implemented in EdgeOCRSample/Models/Activation/ActivationModel.swift.

4. Deactivation​

You can deactivate the license using the deactivate method. The license information stored on the device will be deleted, and the server-side registration will also be removed.

do {
try await licenseAPI.deactivate()
// Deactivation successful
} catch {
// Deactivation failed
print(error.localizedDescription)
}