Skip to content

Commit

Permalink
fix: Swizzling RootUIViewController if ignored
Browse files Browse the repository at this point in the history
The SDK didn't exclude the RootViewController from swizzling when
ignored by the option swizzleClassNameExclude. This is fixed now.

Fixes GH-4385
  • Loading branch information
philipphofmann committed Oct 7, 2024
1 parent 153db3d commit ca85790
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- Linking ongoing trace to crash event (#4393)
- Edge case for swizzleClassNameExclude (#4405): Skip creating transactions for UIViewControllers ignored for swizzling
via the option `swizzleClassNameExclude`.
- Swizzling RootUIViewController if ignored by `swizzleClassNameExclude` (#4407)

## 8.37.0-beta.1

Expand Down
11 changes: 11 additions & 0 deletions Sources/Sentry/SentryUIViewControllerSwizzling.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ @interface UIApplication (SentryUIApplication) <SentryUIApplication>

@interface SentryUIViewControllerSwizzling ()

@property (nonatomic, strong) SentryOptions *options;
@property (nonatomic, strong) SentryInAppLogic *inAppLogic;
@property (nonatomic, strong) SentryDispatchQueueWrapper *dispatchQueue;
@property (nonatomic, strong) id<SentryObjCRuntimeWrapper> objcRuntimeWrapper;
Expand All @@ -56,6 +57,7 @@ - (instancetype)initWithOptions:(SentryOptions *)options
binaryImageCache:(SentryBinaryImageCache *)binaryImageCache
{
if (self = [super init]) {
self.options = options;
self.inAppLogic = [[SentryInAppLogic alloc] initWithInAppIncludes:options.inAppIncludes
inAppExcludes:options.inAppExcludes];
self.dispatchQueue = dispatchQueue;
Expand Down Expand Up @@ -341,6 +343,15 @@ - (void)swizzleViewControllerSubClass:(Class)class
*/
- (BOOL)shouldSwizzleViewController:(Class)class
{
NSString *className = NSStringFromClass(class);

BOOL shouldExcludeClassFromSwizzling = [SentrySwizzleClassNameExclude
shouldExcludeClassWithClassName:className
swizzleClassNameExcludes:self.options.swizzleClassNameExcludes];
if (shouldExcludeClassFromSwizzling) {
return NO;
}

return [self.inAppLogic isClassInApp:class];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ class SentryUIViewControllerSwizzlingTests: XCTestCase {
let subClassFinder: TestSubClassFinder
let processInfoWrapper = SentryNSProcessInfoWrapper()
let binaryImageCache: SentryBinaryImageCache
var options: Options

init() {
subClassFinder = TestSubClassFinder(dispatchQueue: dispatchQueue, objcRuntimeWrapper: objcRuntimeWrapper, swizzleClassNameExcludes: [])
binaryImageCache = SentryDependencyContainer.sharedInstance().binaryImageCache
}

var options: Options {
let options = Options.noIntegrations()

options = Options.noIntegrations()

let imageName = String(
cString: class_getImageName(SentryUIViewControllerSwizzlingTests.self)!,
Expand All @@ -31,8 +30,6 @@ class SentryUIViewControllerSwizzlingTests: XCTestCase {
cString: class_getImageName(ExternalUIViewController.self)!,
encoding: .utf8)! as NSString
options.add(inAppInclude: externalImageName.lastPathComponent)

return options
}

var sut: SentryUIViewControllerSwizzling {
Expand Down Expand Up @@ -98,6 +95,18 @@ class SentryUIViewControllerSwizzlingTests: XCTestCase {
XCTAssertFalse(result)
}

func testShouldNotSwizzle_UIViewControllerExcludedFromSwizzling() {
fixture.options.swizzleClassNameExcludes = ["TestViewController"]

XCTAssertFalse(fixture.sut.shouldSwizzleViewController(TestViewController.self))
}

func testShouldSwizzle_UIViewControllerNotExcludedFromSwizzling() {
fixture.options.swizzleClassNameExcludes = ["TestViewController1"]

XCTAssertTrue(fixture.sut.shouldSwizzleViewController(TestViewController.self))
}

func testUIViewController_loadView_noTransactionBoundToScope() {
fixture.sut.start()
let controller = UIViewController()
Expand Down

0 comments on commit ca85790

Please sign in to comment.