iOS
Contents
The PostHog iOS SDK is a library that you can use to track events, identify users, record session replays, evaluate feature flags, run experiments, build surveys, and more.
This page shows you how to install the SDK and get started with it. If you've already installed the SDK, you can skip ahead to learn about using the features and configuring the SDK.
Installation
PostHog is available through CocoaPods or you can add it as a Swift Package Manager based dependency.
CocoaPods
Swift Package Manager
Add PostHog as a dependency in your Xcode project "Package Dependencies" and select the project target for your app, as appropriate.
For a Swift Package Manager based project, add PostHog as a dependency in your Package.swift file's Package dependencies section:
and then as a dependency for the Package target utilizing PostHog:
Configuration
Configuration is done through the PostHogConfig object. Here's a basic configuration example to get you started.
You can find more advanced configuration options in the configuration page.
Offline behavior
The PostHog iOS SDK will continue to capture events when the device is offline. The events are stored in a queue in the device's file storage and are flushed when the device is online.
- The queue has a maximum size defined by
maxQueueSizein the configuration. - When the queue is full, the oldest event is deleted first.
- The queue is flushed only when the device is online.
You can find the options for configuring the offline behavior in the configuration page.
Using PostHog with application extensions
PostHog supports sharing analytics data between your main app and application extensions (such as widgets, app clips, share extensions, and custom keyboards) through App Groups. This ensures that users maintain the same identity across all parts of your app ecosystem.
By default, each iOS app target stores its data in its own sandboxed directory. This means that if a user interacts with your main app and then uses a widget or extension, PostHog would treat them as two different anonymous users. This can lead to:
- Inflated user counts in your analytics
- Fragmented user journeys
- Difficulty tracking feature adoption across your app ecosystem
Learn more about setting up app groups.
Method swizzling
The PostHog iOS SDK uses method swizzling to intercept and modify method calls at runtime to provide advanced features like screen view tracking, element interactions, session replay, surveys, and more.
Method swizzling is particularly important for accurate session metrics tracking. When disabled, the SDK cannot capture optimal session metrics.
You can learn more about configuring method swizzling in the configuration page.
Next steps
Now that you've installed the SDK, explore the configuration and usage options: