iOS SDK v2 to iOS (Swift) SDK Migration Guide

Learn how to migrate persisted user data from the legacy iOS SDK v2 to the iOS (Swift) SDK.

This section provides instructions for migrating persisted user data from the legacy iOS SDK v2 to the iOS (Swift) SDK to maintain continuity of user identity, session tracking, and custom traits across SDK versions.

Overview

You can migrate the following values from the legacy iOS SDK v2 to the iOS (Swift) SDK:

  • anonymousId
  • userId
  • traits
  • Session-related values
  • App version
  • App build

Note that:

  • The iOS (Swift) SDK does not persist external IDs, so you do not need to migrate them.
  • The iOS SDK v2 does not persist the anonymousId anywhere — it uses the current device’s identifierForVendor instead.

Step 1: Understand legacy SDK storage

The legacy iOS SDK v2 persists userId, traits, app version, app build number, and session-related information under their respective keys in UserDefaults.standard.

The following table explains how to read values from the legacy SDK storage:

FieldLegacy storage keyValue typeNotes
Anonymous ID-UUID
String
anonymousId is not stored anywhere.

Instead, the current device’s identifierForVendor is used.
Traitsrs_traitsJSON
String
Stored as keyed-archived dictionary data.
User IDrs_user_idString-
Session IDrl_session_idNSNumber
UInt64
If not present, there is no active session in the legacy SDK
Manual session statusrl_session_manual_track_statusNSNumber
Boolean
Indicates whether automatic session tracking is enabled
Last event timestamprl_last_event_time_stampNSNumber
Double
Timestamp of the most recent event recorded by the SDK
App version numberrs_application_version_keyStringCFBundleShortVersionString value from info.plist
App build numberrs_application_build_keyStringCFBundleVersion value from info.plist
warning

Data availability

These values are available only if the legacy SDK based app was not removed before installing the app version integrated with the iOS (Swift) SDK.

Step 2: Migration

Follow these steps to migrate the existing values:

  1. Retrieve values from the legacy storage location.
  2. Transform values as needed — see the Storage key mapping table below.
  3. Save values to the iOS (Swift) SDK-specific UserDefaults suite.
  4. Initialize the iOS (Swift) SDK.

Storage key mapping

After retrieving the legacy values, store each value in the iOS (Swift) SDK’s UserDefaults instance using its corresponding key:

FieldLegacy keyiOS (Swift) SDK keyTransformation notes
Anonymous ID-anonymous_idMigrates the current device’s identifierForVendor
User IDrs_user_iduser_idExtract the userId value from the traits JSON string
Traitsrs_traitstraitsStore as JSON string; remove anonymousId, id and userId if present in the retrieved value
Session IDrl_session_idsession_idConverts the value to String format
Automatic session statusrl_session_manual_track_statusis_manual_sessionStores the inverted (toggled) Boolean value
Last activity timerl_last_event_time_stamplast_activity_timeRequires timestamp conversion — see Timestamp conversion section below
External IDrl_external_id-No need to migrate as the iOS (Swift) SDK does not persist external IDs
Application version numberrs_application_version_keyrudder.app_versionThe CFBundleShortVersionString value from info.plist will be migrated as a String
Application build numberrs_application_build_keyrudder.app_buildThe CFBundleVersion value from info.plist will be migrated as an Int

Timestamp conversion

The legacy SDK and iOS (Swift) SDK use different time reference systems for tracking the last activity time:

SDKTime reference
Legacy iOS v2 SDKAbsolute timestamp (Unix epoch time)
iOS (Swift) SDKSystem uptime (time since last device boot)

How to convert the legacy timestamp to the iOS (Swift) SDK format

  1. Read the current timestamp (t_now).
  2. Read the current system uptime (u_now).
  3. Calculate the system boot timestamp:
bootTimestamp = t_now - u_now
  1. Calculate the uptime at which the legacy event occurred:
uptimeAtTimestamp = legacyTimestamp - bootTimestamp
info

Edge case

If the legacy timestamp occurred before the current system boot (meaning the device was restarted after the last legacy SDK event), the conversion is not possible. In this scenario, you can skip migrating the last activity time value.

Migration helper class

RudderStack provides a persistence migration helper class — PersistentMigratorFromV2 — to help you migrate persisted values from earlier versions of the iOS (Swift) SDK.

Note that:

  • The migrator is optional and intended to be used as a reference implementation.
  • You can selectively rely on it based on the values you need to migrate.
  • Make sure this step runs only once during app startup, before the SDK initialization.
  • Alternatively, you can call readPersistence() to inspect the available values before performing the migration.
tip
Tip: Use this helper if you are upgrading from the legacy iOS SDK v2 and want to restore persisted values before initializing the iOS (Swift) SDK.

Usage

Initialize the migrator and call restorePersistence() before initializing the iOS (Swift) SDK:

PersistentMigratorFromV2(writeKey: "swift-sdk-write-key").restorePersistence()

Troubleshooting

IssuePossible causeSolution
Legacy data not foundThe app was uninstalled before migrationNo migration needed — the iOS (Swift) SDK will generate new values automatically
Timestamp conversion failsThe device was rebooted after the last legacy SDK eventSkip last activity time migration — the session will reset naturally
Traits parsing failsCorrupted or invalid JSON in legacy storageLog the error and skip traits migration — other values can still be migrated
User ID not found in traitsUser was never identified in the legacy SDKNo action needed — userId will remain nil until the user is identified

Questions? Contact us by Email or on Slack