DefaultDeviceIdentifier

public actor DefaultDeviceIdentifier : DeviceIdentifier, Sendable

Default implementation that generates and persists a device identifier. Implements DeviceIdentifier protocol to provide a unique identifier for the device. Example usage:

let deviceId = DefaultDeviceIdentifier()
let identifier = try await deviceId.id

// In case of migration from the FR SDK when using a custom Keychain Access Group use the following


func setupDeviceIdentifierWithMigration() async throws -> String {
    // If your legacy FRAuth SDK used a custom keychain access group,
    // specify it here to enable migration
    let configuration = DeviceIdentifierConfiguration(
        keySize: DeviceIdentifierConfiguration.default.keySize,
        keychainAccount: DeviceIdentifierConfiguration.default.keychainAccount,
        useEncryption: DeviceIdentifierConfiguration.default.useEncryption,
        legacyKeychainAccessGroup: "com.test" // Set Legacy Keychain Access Group
    )

    // Initialize with configuration
   let deviceIdentifier = try DefaultDeviceIdentifier(
        configuration: configuration,
        logger: LogManager.standard  // Optional: for debugging migration
    )

    // First access will trigger migration if legacy identifier exists
    let deviceId = try await deviceIdentifier.id
    print("Device ID: \(deviceId)")

    return deviceId
}
  • id Asynchronous

    The unique identifier for the device. This identifier is either retrieved from the keychain or generated if it does not exist.

    Declaration

    Swift

    public var id: String { get async throws }
  • Initializes a new instance of DefaultDeviceIdentifier with custom configuration.

    Declaration

    Swift

    public init(configuration: DeviceIdentifierConfiguration = .default, logger: Logger? = nil) throws

    Parameters

    configuration

    Configuration for device identifier behavior

    logger

    An optional logger to log events. Defaults to nil.

  • Initializes with custom storage (for testing or advanced use cases)

    Note

    When using custom storage, configuration settings for keychain are ignored

    Declaration

    Swift

    public init(configuration: DeviceIdentifierConfiguration = .default, storage: any Storage<DeviceIdentifierImpl>, logger: Logger? = nil)
  • Clears the cached identifier.

    Declaration

    Swift

    public func clearCache()
  • regenerateIdentifier() Asynchronous

    Asynchronously regenerates the device identifier by deleting the existing keychain item. This method cancels any ongoing generation task and clears the cache. Also clears any legacy identifier storage to ensure a completely new identifier is generated.

    Throws

    DeviceIdentifierError if keychain operations fail.

    Declaration

    Swift

    public func regenerateIdentifier() async throws -> String

    Return Value

    The new unique identifier for the device.