iOS (Obj-C) to iOS (Swift) SDK Migration Guide

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

This guide explains how to migrate persisted user data from the legacy iOS (Obj-C) SDKiOS (Obj-C) refers to the legacy RudderStack iOS SDK. Note that it will be deprecated soon.

For new implementations, use the iOS (Swift) SDK instead.
to the iOS (Swift) SDK so that your users maintain their identity when transitioning from the legacy iOS (Obj-C) SDK to the iOS (Swift) SDK.

Pre-migration considerations

You can migrate the following values from the legacy iOS (Obj-C) SDK to the iOS (Swift) SDK:

  • anonymousId
  • userId
  • traits
  • Session tracking status
  • App version
  • App build

Also, note that:

  • Events are not migrated in this process
  • The iOS (Swift) SDK does not persist external IDs, so you do not need to migrate them
  • The iOS (Swift) SDK does not support opt status, opt in time, and opt out time
  • The iOS (Swift) SDK manages server config independently, so you do not need to migrate the server config last update value

Migration process overview

warning

Note that:

  • You must run the migration before initializing the iOS (Swift) SDK. The SDK reads persisted values on initialization, so migrated data must already be in place.
  • Configure the migration code so that it runs only once.
  1. Retrieve values from the legacy iOS (Obj-C) SDK 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.

For more details, refer to the migration helper class available in the iOS (Swift) SDK sample app.

Step 1: Understand legacy SDK storage

The legacy iOS (Obj-C) SDK persists anonymousId, userId, traits, app version, app build number, and session-related information under their respective keys. This data is stored in a property list file named rsDefaultsPersistence.plist.

info

Version note

If you are using an SDK version earlier than v1.25.2, retrieve the values from UserDefaults.standard instead of the rsDefaultsPersistence.plist file.

Step 2: Review legacy SDK storage keys

warning

Data availability

The legacy values are available only if the legacy SDK-based app was not removed before installing the app version that integrates the iOS (Swift) SDK.

Use the following table to read values from the legacy SDK storage:

FieldLegacy storage keyExpected data type
Notes
Anonymous IDrl_anonymous_idUUID
String
-
Traitsrl_traitsJSON
String
Contains anonymousId, userId, and id along with other trait values
User IDrl_traitsStringThe userId value is embedded within the traits JSON string
Session IDrl_session_idNSNumber
UInt64
If not present, there is no active session in the legacy SDK
Automatic session trackingrl_session_auto_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
Application version numberrl_application_version_keyStringCFBundleShortVersionString value from info.plist
Application build numberrl_application_build_keyStringCFBundleVersion value from info.plist

Step 3: Map values to iOS (Swift) SDK

Once you retrieve the legacy values and have access to the iOS (Swift) SDK’s UserDefaults instance, store each value using its corresponding key:

Storage key mapping

FieldLegacy keyiOS (Swift) SDK keyTransformation notes
Anonymous IDrl_anonymous_idanonymous_idDirect migration — no transformation required
User IDrl_traitsuser_idExtract the userId value from the traits JSON string
Traitsrl_traitstraitsStore as JSON string

Remove anonymousId, id, and userId if present in the retrieved value
Session IDrl_session_idsession_idConvert the value to string format
Automatic session statusrl_session_auto_track_statusis_manual_sessionStore the inverted (toggled) Boolean value
Last activity timerl_last_event_time_stamplast_activity_timeRequires timestamp conversion — see Timestamp conversion
External IDrl_external_id-No migration needed — the iOS (Swift) SDK does not persist external IDs
Application version numberrl_application_version_keyrudder.app_versionCFBundleShortVersionString value from info.plist is migrated as a string
Application build numberrl_application_build_keyrudder.app_buildCFBundleVersion value from info.plist is migrated as an integer

Step 4: Convert last activity timestamps

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

SDKTime reference
Legacy iOS (Obj-C) SDKAbsolute timestamp (Unix epoch time) in seconds.
iOS (Swift) SDKAbsolute timestamp (Unix epoch time) in milliseconds.

Conversion steps

  1. Read the legacy timestamp (legacyTimestamp) in seconds.
  2. Validate the timestamp — it must be greater than 0 and not a future timestamp.
  3. Convert to milliseconds:
lastActivityTime = legacyTimestamp * 1000
info

Edge case

If the legacy timestamp is invalid (zero, negative, or a future timestamp), then conversion is not possible. In such cases, skip migrating the last activity time.

Step 5: Apply values and initialize the iOS (Swift) SDK

Once you complete the above steps, save the transformed values in the iOS (Swift) SDK’s UserDefaults suite and then initialize the iOS (Swift) SDK.

Migration helper class

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

tip
Tip: Use this helper if you are upgrading from the iOS (Obj-C) SDK and want to restore persisted values before the iOS (Swift) SDK initializes.

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

Usage

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

PersistentMigratorFromV1(writeKey: "<IOS_SOURCE_WRITE_KEY>").restorePersistence()

Troubleshooting

IssuePossible causeSolution
Legacy data not foundApp was uninstalled before migrationNo migration needed — the iOS (Swift) SDK generates new values automatically
Timestamp conversion failsThe device was rebooted after the last legacy SDK eventSkip last activity time migration — the session resets naturally
Traits parsing failsCorrupt or invalid JSON in legacy storageLog the error and skip traits migration — you can still migrate other values
User ID not found in traitsThe user was never identified in the legacy iOS (Obj-C) SDKNo action needed — userId remains nil until the user is identified

Questions? We're here to help.

Join the RudderStack Slack community or email us for support