Skip to content

Commit

Permalink
Swizzling of view controllers loadView that dont implement loadVi…
Browse files Browse the repository at this point in the history
…ew` (#4071)

Stop swizzling loadView function of UIViewControllers that doesn't implement it
  • Loading branch information
brustolin authored Jun 14, 2024
1 parent 4090908 commit e19cca3
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Fixes

- Fix potential deadlock in app hang detection (#4063)
- Swizzling of view controllers `loadView` that don`t implement `loadView` (#4071)

## 8.29.0

Expand Down
2 changes: 1 addition & 1 deletion Samples/iOS-Swift/iOS-Swift/Tools/UIAssert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class UIAssert {
return
}

let steps = stepsToCheck ?? ["loadView", "viewDidLoad", "viewWillAppear", "viewDidAppear"]
let steps = stepsToCheck ?? ["viewDidLoad", "viewWillAppear", "viewDidAppear"]
var missing = [String]()

steps.forEach { spanDescription in
Expand Down
12 changes: 7 additions & 5 deletions Sources/Sentry/SentryUIViewControllerSwizzling.m
Original file line number Diff line number Diff line change
Expand Up @@ -348,13 +348,15 @@ - (BOOL)shouldSwizzleViewController:(Class)class

- (void)swizzleLoadView:(Class)class
{
// The UIViewController only searches for a nib file if you do not override the loadView method.
// Loading a Nib file is done automatically during `loadView` in the UIViewController
// or other native view controllers.
// When swizzling the loadView of a custom UIViewController, the UIViewController doesn't search
// for a nib file and doesn't load a view. This would lead to crashes as no view is loaded. As a
// workaround, we skip swizzling the loadView and accept that the SKD doesn't create a span for
// loadView if the UIViewController doesn't implement it.
// for a nib file and doesn't load a view. This would lead to crashes as no view is loaded.
// By checking the implementation pointer of `loadView` from the current class with
// the implementation pointer of its parent class, we can determine if current class
// has a custom implementation of it, therefore it's safe to swizzle it.
SEL selector = NSSelectorFromString(@"loadView");
IMP viewControllerImp = class_getMethodImplementation([UIViewController class], selector);
IMP viewControllerImp = class_getMethodImplementation([class superclass], selector);
IMP classLoadViewImp = class_getMethodImplementation(class, selector);
if (viewControllerImp == classLoadViewImp) {
return;
Expand Down

0 comments on commit e19cca3

Please sign in to comment.