Skip to content

Commit e19cca3

Browse files
authored
Swizzling of view controllers loadView that dont implement loadView` (#4071)
Stop swizzling loadView function of UIViewControllers that doesn't implement it
1 parent 4090908 commit e19cca3

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Fixes
66

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

910
## 8.29.0
1011

Samples/iOS-Swift/iOS-Swift/Tools/UIAssert.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class UIAssert {
8484
return
8585
}
8686

87-
let steps = stepsToCheck ?? ["loadView", "viewDidLoad", "viewWillAppear", "viewDidAppear"]
87+
let steps = stepsToCheck ?? ["viewDidLoad", "viewWillAppear", "viewDidAppear"]
8888
var missing = [String]()
8989

9090
steps.forEach { spanDescription in

Sources/Sentry/SentryUIViewControllerSwizzling.m

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -348,13 +348,15 @@ - (BOOL)shouldSwizzleViewController:(Class)class
348348

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

0 commit comments

Comments
 (0)