Skip to content

Avoid swizzling #362

Closed
Closed

Description

@cbg-dev-k

Problem Statement

Hello

Our team is looking into PostHog, my biggest issue is the swizzling of system functions in a third party SDK.

I understand PostHog wants to be the only analytics tool you need, but in reality you're not working in a vacuum. And the current SDK swizzles a lot; layoutSubviews, viewDidAppear, Application sendAction & sendEvent, scrollViews, urlSession, ...

I didn't find any settings to avoid swizzling, are there any plans to provide a 'light version'? Does anyone know how badly functionality would be impacted by disabling swizzling?

For reference, our crash reporting tool also uses swizzling but provides a global disable setting and even allows string matching to disable specific instances.

Thanks for any input!

Solution Brainstorm

No response

Activity

  1. ioannisj commented on Jun 23, 2025

    @ioannisj
    Contributor

    Hi there 👋 Thanks for raising this!

    You’re right that our iOS SDK uses method swizzling in several places to enable some of the advanced features we provide like session replay, autocapture, surveys and screen view capture.

    At the moment, we don't provide any global "disable swizzling" flag - but could be something worth considering. Having said that, swizzling is only executed when the relevant features mentioned above are enabled in the SDK config. If you want to avoid or reduce swizzling you can just disable those features

    config.sessionReplay = false
    config.surveys = false
    config.captureElementInteractions = false
    config.captureScreenViews = false

    The above will remove almost all swizzling the SDK does with the exception of Session Manager which uses swizzling to detect idle time - could easily be refactored when the global flag is introduced, however.

  2. andrewtheis commented on Sep 24, 2025

    @andrewtheis

    @ioannisj

    +1 to being allowed to disable Swizzling completely. Even with all above options disabled, it does appear to introduce a non-negligible performance hit, particularly with SwiftUI. This shows it taking up roughly ~4% of CPU cycles on a simple navigation tap (on an iPhone 17 Pro, I imagine it's worse on older devices).

    With PostHog Enabled:
    Image

    With PostHog Disabled:
    Image

  3. ioannisj commented on Sep 24, 2025

    @ioannisj
    Contributor

    Hey @andrewtheis, this is with the latest SDK version correct?

  4. andrewtheis commented on Sep 24, 2025

    @andrewtheis

    @ioannisj yes, 3.31.0

  5. ioannisj commented on Oct 3, 2025

    @ioannisj
    Contributor

    Hey guys @andrewtheis @cbg-dev-k. Just wanted to let you know that I'm actively working on adding a flag for disabling swizzling all together. As well as looking into the high cpu impact mentioned here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions