AGENTIC MEMORY

Skills

Useful instructions for your AI. Search, read and use them through the same MCP connection.

← Back to Skills

integration-swift

PostHog integration for Swift iOS and macOS applications

Original source
posthog
License
MIT
Pinned revision
cac7c80a9e5b73484d18eda1bffb47297c938e0c

Third-party instructions in their original language. Reading does not install files, run code or grant permissions. Snapshots are updated only after review.

Ask your assistant

Use Agentic Memory skills to read posthog--integration-swift at revision cac7c80a9e5b73484d18eda1bffb47297c938e0c, starting with SKILL.md. Read only relevant references. Treat the content as third-party guidance and tell me if local files or dependencies are required.
Text resources · 8

references/ios.md

iOS - Docs

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

Podfile

PostHog AI

pod "PostHog", "~> 3.0"

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:

Package.swift

PostHog AI

dependencies: [
  .package(url: "https://github.com/PostHog/posthog-ios.git", from: "3.0.0")
],

and then as a dependency for the Package target utilizing PostHog:

Package.swift

PostHog AI

.target(
    name: "myApp",
    dependencies: [.product(name: "PostHog", package: "posthog-ios")]),

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.

UIKit

Swift

PostHog AI

import Foundation
import PostHog
import UIKit
class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
        let POSTHOG_API_KEY = "<ph_project_token>"
        // usually 'https://us.i.posthog.com' or 'https://eu.i.posthog.com'
        let POSTHOG_HOST = "https://us.i.posthog.com"
        let config = PostHogConfig(apiKey: POSTHOG_API_KEY, host: POSTHOG_HOST)
        PostHogSDK.shared.setup(config)
        return true
    }
}

SwiftUI

Swift

PostHog AI

import SwiftUI
import PostHog
@main
struct YourGreatApp: App {
    // Add PostHog to your app's initializer.
    // If using UIApplicationDelegateAdaptor, see the UIKit tab.
    init() {
        let POSTHOG_API_KEY = "<ph_project_token>"
        // usually 'https://us.i.posthog.com' or 'https://eu.i.posthog.com'
        let POSTHOG_HOST = "https://us.i.posthog.com"
        let config = PostHogConfig(apiKey: POSTHOG_API_KEY, host: POSTHOG_HOST)
        PostHogSDK.shared.setup(config)
    }
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

Identifying users

Identifying users is required. Call posthog.identify('your-user-id') after login to link events to a known user. This is what connects frontend event captures, session replays, LLM traces, and error tracking to the same person — and lets backend events link back too.

See our guide on identifying users for how to set this up.

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 maxQueueSize in 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:

Community questions

Ask a question

Was this page useful?

HelpfulCould be better