Skip to main content
This guide will walk you through implementing session management in your Swift app using the Dynamic Swift SDK. You’ll learn how to manage authentication state, handle reactive UI updates with Combine, and create a seamless user experience.

Overview

Session management is a crucial part of any Web3 app. The Dynamic Swift SDK provides powerful Combine-based publishers for managing user sessions, authentication state, and wallet updates. This guide covers the practical implementation patterns you’ll need to build a robust session management system.

Key Concepts

Reactive State with Combine

The SDK provides Combine publishers that automatically emit updates when state changes:
  • authenticatedUserChanges - Emits when user logs in or out
  • tokenChanges - Emits when the auth token changes
  • userWalletsChanges - Emits when wallets are created or updated

Automatic UI Updates

By subscribing to these publishers in your SwiftUI views, the UI automatically updates when:
  • Users log in or out
  • Authentication tokens refresh
  • Wallets are connected or created
  • Network connections change

Implementation Patterns

1. Basic Session Management

Start with a simple session management setup using a ViewModel:

2. Complete Session Manager

For production apps, implement a comprehensive session manager:

3. Using Session Manager in SwiftUI

4. Navigation Based on Auth State

Handle navigation when authentication state changes:

4. Advanced Session Observer

For more complex apps, use a dedicated session observer:

5. Callback-Based Navigation Pattern

An alternative pattern using callbacks:

Best Practices

1. Always Use Main Thread for UI Updates

2. Initialization Order

Always initialize the SDK at app launch:

2. Store Cancellables Properly

3. Check Initial State

Always check the current state before setting up listeners:

4. Handle Wallet Creation Loading State

Wallets are created asynchronously after authentication:

5. Persistent Session Management (Optional)

For apps that need session persistence:

6. App Lifecycle Management

Handle app lifecycle events properly:

Troubleshooting

Common Issues

State not updating
  • Ensure you’re calling .receive(on: DispatchQueue.main) before .sink
  • Verify the cancellable is stored in cancellables set
  • Ensure you’re subscribing to the reactive Combine publishers (authenticatedUserChanges, userWalletsChanges, etc.)
  • Verify your view models use @StateObject or @ObservedObject and store cancellables properly
  • Check that the SDK is initialized before accessing DynamicSDK.instance()
Navigation not working after login
  • Make sure you’re subscribed to authenticatedUserChanges before the user authenticates
  • Check that your navigation logic handles the case where user is already authenticated
Wallets not appearing
  • Wallets are created asynchronously after authentication
  • Subscribe to userWalletsChanges to receive updates
  • Check that embedded wallets are enabled in your Dynamic dashboard
  • Use @StateObject for view models that observe SDK state
  • Ensure Combine publishers are properly subscribed and cancellables are stored
  • Make sure to use @MainActor for view models that update UI

Debug Session State

Add logging to understand state changes:

What’s Next

Now that you have session management set up, you can:
  1. Authentication Guide - Implement user authentication flows
  2. Wallet Operations - Work with wallet balances and signing
  3. Networks - Configure blockchain networks
Last modified on February 2, 2026