Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swizzling of view controllers loadView that dont implement loadView` #4071

Merged
merged 4 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
brustolin marked this conversation as resolved.
Show resolved Hide resolved
IMP classLoadViewImp = class_getMethodImplementation(class, selector);
if (viewControllerImp == classLoadViewImp) {
return;
Expand Down
Loading