Method Swizzling

User/Repro Steps

Instabug automatically tracks steps users have taken in the app before a bug is reported or a crash has occurred. This gives you great insights on how to reproduce the issue your users have encountered. Note that the maximum number of user steps sent with each report is 100.

To be able to capture the user steps, we need to do some method swizzling. We take an approach to swizzling that is absolutely safe and does not impact your app negatively in any way. In all the methods we swizzle, we call the original implementation to make sure we never break your app.
Method swizzling done in the Instabug SDK never impacts the performance of your app. All the methods we swizzle only log events, and this is done in a low-priority background queue. As displayed in the following message the sizzling overhead is 1.0 ms.

683

As of the latest version of the SDK, we swizzle the following methods:

  • - [UIApplication sendAction:to:from:forEvent:]: we log the action, target, sender and current view.
  • - [UIApplication sendEvent:]: we log the event type (touch, motion, etc). For privacy, we ignore any touch events on the keyboard.

To be able to keep track of the top visible view controller, we also swizzle the following methods:

  • - [UIWindow resignKeyWindow]
  • - [UIWindow becomeKeyWindow]
  • - [UIViewController viewDidAppear:]
  • - [UIViewController viewDidDisappear:]

Finally, only when running on the iOS simulator, we swizzle - [UIResponder motionEnded:withEvent:] to be able to respond to shake gestures.

Disabling Method Swizzling

All method swizzling done by the iOS SDK is absolutely safe and has no negative impact on the performance of your app, but if you decide you don’t want any method swizzling in your app, you can disable it by using the following method. Please note that the following method should be called right before Instabug is initialized with startWithToken.

Instabug.disableMethodSwizzling()
[Instabug disableMethodSwizzling];

🚧

Effects of disabling swizzling

Disabling swizzling will prevent the SDK from doing the following:

  • Log User Steps
  • Log Repro Steps
  • Capture and show touch annotations on screen recordings
  • Using the shake gesture on simulators

Manually Logging Touch Events

If method swizzling is disabled, you can manually log touch events by following the below steps.

Log viewDidAppear

In order to group events together for the Repro Steps, you'll need to call the below method whenever a new view controller appears (whenever viewDidAppear is called):

Instabug.logViewDidAppearEvent("view name")
[Instabug logViewDidAppearEvent:@"view name"];

Log Touch Events

In order to log the touch events that occur on your app manually, you should call the following method while passing to it the event type and the view name. This is required for both user and repro steps:

Instabug.logTouch(.tap, viewName: "view name")
[Instabug logTouchEvent:IBGUIEventTypeTap viewName:@"view name"];

The available event types are as follows:

.tap
.forceTouch
.longPress
.pinch
.swipe
.scroll
IBGUIEventTypeTap
IBGUIEventTypeForceTouch
IBGUIEventTypeLongPress
IBGUIEventTypePinch
IBGUIEventTypeSwipe
IBGUIEventTypeScroll