Skip to content

Commit bc4bb46

Browse files
ref: Convert SentryViewHierarchyProvider to Swift (#6150)
* ref: Convert SentryViewHierarchyProvider to Swift * Cleanup * Fix tests * Update Tests/SentryTests/Integrations/ViewHierarchy/TestSentryViewHierarchyProvider.swift Co-authored-by: Philip Niedertscheider <[email protected]> * Update Tests/SentryTests/Integrations/ViewHierarchy/TestSentryViewHierarchyProvider.swift Co-authored-by: Philip Niedertscheider <[email protected]> * Update Tests/SentryTests/Integrations/ViewHierarchy/TestSentryViewHierarchyProvider.swift Co-authored-by: Philip Niedertscheider <[email protected]> * Spelling --------- Co-authored-by: Philip Niedertscheider <[email protected]>
1 parent 4a7a005 commit bc4bb46

14 files changed

+149
-122
lines changed

Sentry.xcodeproj/project.pbxproj

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
0A2D8D8728992260008720F6 /* SentryBaseIntegrationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A2D8D8628992260008720F6 /* SentryBaseIntegrationTests.swift */; };
3636
0A2D8D9628997845008720F6 /* NSLocale+Sentry.m in Sources */ = {isa = PBXBuildFile; fileRef = 0A2D8D9428997845008720F6 /* NSLocale+Sentry.m */; };
3737
0A2D8D9828997887008720F6 /* NSLocale+Sentry.h in Headers */ = {isa = PBXBuildFile; fileRef = 0A2D8D9728997887008720F6 /* NSLocale+Sentry.h */; };
38-
0A2D8DA8289BC905008720F6 /* SentryViewHierarchyProvider.h in Headers */ = {isa = PBXBuildFile; fileRef = 0A2D8DA6289BC905008720F6 /* SentryViewHierarchyProvider.h */; };
39-
0A2D8DA9289BC905008720F6 /* SentryViewHierarchyProvider.m in Sources */ = {isa = PBXBuildFile; fileRef = 0A2D8DA7289BC905008720F6 /* SentryViewHierarchyProvider.m */; };
38+
0A2D8DA8289BC905008720F6 /* SentryViewHierarchyProviderHelper.h in Headers */ = {isa = PBXBuildFile; fileRef = 0A2D8DA6289BC905008720F6 /* SentryViewHierarchyProviderHelper.h */; };
39+
0A2D8DA9289BC905008720F6 /* SentryViewHierarchyProviderHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = 0A2D8DA7289BC905008720F6 /* SentryViewHierarchyProviderHelper.m */; };
4040
0A5370A128A3EC2400B2DCDE /* SentryViewHierarchyProviderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A5370A028A3EC2400B2DCDE /* SentryViewHierarchyProviderTests.swift */; };
4141
0A56DA5F28ABA01B00C400D5 /* SentryTransactionContext+Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 0A56DA5E28ABA01B00C400D5 /* SentryTransactionContext+Private.h */; };
4242
0A80E433291017C300095219 /* SentryWatchdogTerminationScopeObserver.m in Sources */ = {isa = PBXBuildFile; fileRef = 0A80E432291017C300095219 /* SentryWatchdogTerminationScopeObserver.m */; };
@@ -1119,6 +1119,7 @@
11191119
FAEC270E2DF3526000878871 /* SentryUserFeedback.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAEC270D2DF3526000878871 /* SentryUserFeedback.swift */; };
11201120
FAEC273D2DF3933A00878871 /* NSData+Unzip.m in Sources */ = {isa = PBXBuildFile; fileRef = FAEC273C2DF3933200878871 /* NSData+Unzip.m */; };
11211121
FAEEC0522E75E55F00E79CA9 /* SentrySerializationSwift.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAEEC04C2E75E55A00E79CA9 /* SentrySerializationSwift.swift */; };
1122+
FAEEBFE22E736D4B00E79CA9 /* SentryViewHierarchyProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAEEBFDC2E736D4100E79CA9 /* SentryViewHierarchyProvider.swift */; };
11221123
FAEFA12F2E4FAE1900C431D9 /* SentrySDKSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAEFA1292E4FAE1700C431D9 /* SentrySDKSettings.swift */; };
11231124
FAF120182E70C08F006E1DA3 /* SentryEnvelopeHeaderHelper.h in Headers */ = {isa = PBXBuildFile; fileRef = FAF120122E70C088006E1DA3 /* SentryEnvelopeHeaderHelper.h */; };
11241125
FAF1201A2E70C0EE006E1DA3 /* SentryEnvelopeHeaderHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = FAF120192E70C0EA006E1DA3 /* SentryEnvelopeHeaderHelper.m */; };
@@ -1270,8 +1271,8 @@
12701271
0A2D8D8628992260008720F6 /* SentryBaseIntegrationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryBaseIntegrationTests.swift; sourceTree = "<group>"; };
12711272
0A2D8D9428997845008720F6 /* NSLocale+Sentry.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSLocale+Sentry.m"; sourceTree = "<group>"; };
12721273
0A2D8D9728997887008720F6 /* NSLocale+Sentry.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "NSLocale+Sentry.h"; path = "include/NSLocale+Sentry.h"; sourceTree = "<group>"; };
1273-
0A2D8DA6289BC905008720F6 /* SentryViewHierarchyProvider.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryViewHierarchyProvider.h; path = include/SentryViewHierarchyProvider.h; sourceTree = "<group>"; };
1274-
0A2D8DA7289BC905008720F6 /* SentryViewHierarchyProvider.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryViewHierarchyProvider.m; sourceTree = "<group>"; };
1274+
0A2D8DA6289BC905008720F6 /* SentryViewHierarchyProviderHelper.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryViewHierarchyProviderHelper.h; path = include/SentryViewHierarchyProviderHelper.h; sourceTree = "<group>"; };
1275+
0A2D8DA7289BC905008720F6 /* SentryViewHierarchyProviderHelper.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryViewHierarchyProviderHelper.m; sourceTree = "<group>"; };
12751276
0A5370A028A3EC2400B2DCDE /* SentryViewHierarchyProviderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryViewHierarchyProviderTests.swift; sourceTree = "<group>"; };
12761277
0A56DA5E28ABA01B00C400D5 /* SentryTransactionContext+Private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "SentryTransactionContext+Private.h"; path = "include/SentryTransactionContext+Private.h"; sourceTree = "<group>"; };
12771278
0A80E432291017C300095219 /* SentryWatchdogTerminationScopeObserver.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryWatchdogTerminationScopeObserver.m; sourceTree = "<group>"; };
@@ -2463,6 +2464,7 @@
24632464
FAEC273C2DF3933200878871 /* NSData+Unzip.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSData+Unzip.m"; sourceTree = "<group>"; };
24642465
FAEC273E2DF393E000878871 /* NSData+Unzip.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NSData+Unzip.h"; sourceTree = "<group>"; };
24652466
FAEEC04C2E75E55A00E79CA9 /* SentrySerializationSwift.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentrySerializationSwift.swift; sourceTree = "<group>"; };
2467+
FAEEBFDC2E736D4100E79CA9 /* SentryViewHierarchyProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryViewHierarchyProvider.swift; sourceTree = "<group>"; };
24662468
FAEFA1292E4FAE1700C431D9 /* SentrySDKSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentrySDKSettings.swift; sourceTree = "<group>"; };
24672469
FAF120122E70C088006E1DA3 /* SentryEnvelopeHeaderHelper.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryEnvelopeHeaderHelper.h; path = include/SentryEnvelopeHeaderHelper.h; sourceTree = "<group>"; };
24682470
FAF120192E70C0EA006E1DA3 /* SentryEnvelopeHeaderHelper.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryEnvelopeHeaderHelper.m; sourceTree = "<group>"; };
@@ -4124,8 +4126,8 @@
41244126
children = (
41254127
D8F6A24A2885515B00320515 /* SentryPredicateDescriptor.h */,
41264128
D8F6A2452885512100320515 /* SentryPredicateDescriptor.m */,
4127-
0A2D8DA6289BC905008720F6 /* SentryViewHierarchyProvider.h */,
4128-
0A2D8DA7289BC905008720F6 /* SentryViewHierarchyProvider.m */,
4129+
0A2D8DA6289BC905008720F6 /* SentryViewHierarchyProviderHelper.h */,
4130+
0A2D8DA7289BC905008720F6 /* SentryViewHierarchyProviderHelper.m */,
41294131
D83D07992B7F9D1C00CC9674 /* SentryMsgPackSerializer.h */,
41304132
D83D079A2B7F9D1C00CC9674 /* SentryMsgPackSerializer.m */,
41314133
D43A2A0F2DD47FB700114724 /* SentryWeakMap.h */,
@@ -4475,6 +4477,7 @@
44754477
D856272A2A374A6800FB8062 /* Tools */ = {
44764478
isa = PBXGroup;
44774479
children = (
4480+
FAEEBFDC2E736D4100E79CA9 /* SentryViewHierarchyProvider.swift */,
44784481
FA94E6B12E6D265500576666 /* SentryEnvelope.swift */,
44794482
FA94E68B2E6B92BE00576666 /* SentryClientReport.swift */,
44804483
FA3AEE772E68E2830092283E /* SentryEnvelopeHeader.swift */,
@@ -4934,7 +4937,7 @@
49344937
84354E1129BF944900CDBB8B /* SentryProfileTimeseries.h in Headers */,
49354938
D8ACE3CD2762187D00F5A213 /* SentryNSDataSwizzling.h in Headers */,
49364939
7B08A3452924CF6C0059603A /* SentryMetricKitIntegration.h in Headers */,
4937-
0A2D8DA8289BC905008720F6 /* SentryViewHierarchyProvider.h in Headers */,
4940+
0A2D8DA8289BC905008720F6 /* SentryViewHierarchyProviderHelper.h in Headers */,
49384941
8EAE980C261E9F530073B6B3 /* SentryUIViewControllerPerformanceTracker.h in Headers */,
49394942
63FE717D20DA4C1100CDBAE8 /* SentryCrashCachedData.h in Headers */,
49404943
03BCC38A27E1BF49003232C7 /* SentryTime.h in Headers */,
@@ -5691,6 +5694,7 @@
56915694
84E13B842CBF1D91003B52EC /* SentryUserFeedbackWidgetButtonMegaphoneIconView.swift in Sources */,
56925695
63FE715920DA4C1100CDBAE8 /* SentryCrashCPU_x86_32.c in Sources */,
56935696
92235CAE2E15549C00865983 /* SentryLogger.swift in Sources */,
5697+
FAEEBFE22E736D4B00E79CA9 /* SentryViewHierarchyProvider.swift in Sources */,
56945698
D8C66A372A77B1F70015696A /* SentryPropagationContext.m in Sources */,
56955699
7BE912AD272162D900E49E62 /* SentryNoOpSpan.m in Sources */,
56965700
63FE710D20DA4C1000CDBAE8 /* SentryCrashStackCursor_MachineContext.c in Sources */,
@@ -5881,7 +5885,7 @@
58815885
7BA61CBB247BC5D800C130A8 /* SentryCrashDefaultBinaryImageProvider.m in Sources */,
58825886
63FE713120DA4C1100CDBAE8 /* SentryCrashDynamicLinker.c in Sources */,
58835887
03F84D3527DD4191008FE43F /* SentryThreadHandle.cpp in Sources */,
5884-
0A2D8DA9289BC905008720F6 /* SentryViewHierarchyProvider.m in Sources */,
5888+
0A2D8DA9289BC905008720F6 /* SentryViewHierarchyProviderHelper.m in Sources */,
58855889
D84D2CDD2C2BF7370011AF8A /* SentryReplayEvent.swift in Sources */,
58865890
D8BC28CC2BFF78220054DA4D /* SentryRRWebTouchEvent.swift in Sources */,
58875891
D452FC592DDB4B1700AFF56F /* SentryWatchdogTerminationBreadcrumbProcessor.m in Sources */,

Sources/Sentry/PrivateSentrySDKOnly.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#import "SentrySessionReplayIntegration+Private.h"
1818
#import "SentrySwift.h"
1919
#import "SentryUser+Private.h"
20-
#import "SentryViewHierarchyProvider.h"
2120
#import <SentryBreadcrumb.h>
2221
#import <SentryDependencyContainer.h>
2322
#import <SentryFramesTracker.h>

Sources/Sentry/SentryDependencyContainer.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
#if SENTRY_HAS_UIKIT
3434
# import "SentryANRTrackerV2.h"
3535
# import "SentryFramesTracker.h"
36-
# import <SentryViewHierarchyProvider.h>
3736
# import <SentryWatchdogTerminationBreadcrumbProcessor.h>
3837
#endif // SENTRY_HAS_UIKIT
3938

Sources/Sentry/SentryViewHierarchyIntegration.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# import "SentryHub+Private.h"
1010
# import "SentryOptions.h"
1111
# import "SentrySDK+Private.h"
12-
# import "SentryViewHierarchyProvider.h"
12+
# import "SentrySwift.h"
1313
# if SENTRY_HAS_METRIC_KIT
1414
# import "SentryMetricKitIntegration.h"
1515
# endif // SENTRY_HAS_METRIC_KIT

Sources/Sentry/SentryViewHierarchyProvider.m renamed to Sources/Sentry/SentryViewHierarchyProviderHelper.m

Lines changed: 28 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#import "SentryViewHierarchyProvider.h"
1+
#import "SentryViewHierarchyProviderHelper.h"
22

33
#if SENTRY_HAS_UIKIT
44

@@ -24,66 +24,37 @@
2424
return SentryCrashJSON_OK;
2525
}
2626

27-
@interface SentryViewHierarchyProvider ()
27+
@implementation SentryViewHierarchyProviderHelper
2828

29-
@property (nonatomic, strong) SentryDispatchQueueWrapper *dispatchQueueWrapper;
30-
@property (nonatomic, strong) id<SentryApplication> sentryUIApplication;
31-
32-
@end
33-
34-
@implementation SentryViewHierarchyProvider
35-
36-
- (instancetype)initWithDispatchQueueWrapper:(SentryDispatchQueueWrapper *)dispatchQueueWrapper
37-
sentryUIApplication:(id<SentryApplication>)sentryUIApplication
29+
+ (BOOL)saveViewHierarchy:(NSString *)filePath
30+
windows:(NSArray<UIWindow *> *)windows
31+
reportAccessibilityIdentifier:(BOOL)reportAccessibilityIdentifier
3832
{
39-
if (self = [super init]) {
40-
self.reportAccessibilityIdentifier = YES;
41-
self.dispatchQueueWrapper = dispatchQueueWrapper;
42-
self.sentryUIApplication = sentryUIApplication;
43-
}
44-
return self;
45-
}
46-
47-
- (BOOL)saveViewHierarchy:(NSString *)filePath
48-
{
49-
NSArray<UIWindow *> *windows = [self.sentryUIApplication getWindows];
50-
5133
const char *path = [filePath UTF8String];
5234
int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0644);
5335
if (fd < 0) {
5436
SENTRY_LOG_DEBUG(@"Could not open file %s for writing: %s", path, strerror(errno));
5537
return NO;
5638
}
5739

58-
BOOL result = [self processViewHierarchy:windows addFunction:writeJSONDataToFile userData:&fd];
40+
BOOL result = [self processViewHierarchy:windows
41+
reportAccessibilityIdentifier:reportAccessibilityIdentifier
42+
addFunction:writeJSONDataToFile
43+
userData:&fd];
5944

6045
close(fd);
6146
return result;
6247
}
6348

64-
- (NSData *)appViewHierarchyFromMainThread
65-
{
66-
__block NSData *result;
67-
68-
void (^fetchViewHierarchy)(void) = ^{ result = [self appViewHierarchy]; };
69-
70-
SENTRY_LOG_INFO(@"Starting to fetch the view hierarchy from the main thread.");
71-
72-
[self.dispatchQueueWrapper dispatchSyncOnMainQueue:fetchViewHierarchy];
73-
74-
SENTRY_LOG_INFO(@"Finished fetching the view hierarchy from the main thread.");
75-
76-
return result;
77-
}
78-
79-
- (NSData *)appViewHierarchy
49+
+ (NSData *)appViewHierarchyFrom:(NSArray<UIWindow *> *)windows
50+
reportAccessibilityIdentifier:(BOOL)reportAccessibilityIdentifier
8051
{
8152
NSMutableData *result = [[NSMutableData alloc] init];
82-
NSArray<UIWindow *> *windows = [self.sentryUIApplication getWindows];
8353

8454
if (![self processViewHierarchy:windows
85-
addFunction:writeJSONDataToMemory
86-
userData:(__bridge void *)(result)]) {
55+
reportAccessibilityIdentifier:reportAccessibilityIdentifier
56+
addFunction:writeJSONDataToMemory
57+
userData:(__bridge void *)(result)]) {
8758

8859
result = nil;
8960
}
@@ -95,9 +66,10 @@ - (NSData *)appViewHierarchy
9566
if ((result = (code)) != SentryCrashJSON_OK) \
9667
return result;
9768

98-
- (BOOL)processViewHierarchy:(NSArray<UIView *> *)windows
99-
addFunction:(SentryCrashJSONAddDataFunc)addJSONDataFunc
100-
userData:(void *const)userData
69+
+ (BOOL)processViewHierarchy:(NSArray<UIView *> *)windows
70+
reportAccessibilityIdentifier:(BOOL)reportAccessibilityIdentifier
71+
addFunction:(SentryCrashJSONAddDataFunc)addJSONDataFunc
72+
userData:(void *const)userData
10173
{
10274

10375
__block SentryCrashJSONEncodeContext JSONContext;
@@ -113,7 +85,9 @@ - (BOOL)processViewHierarchy:(NSArray<UIView *> *)windows
11385
tryJson(sentrycrashjson_beginArray(&JSONContext, "windows"));
11486

11587
for (UIView *window in windows) {
116-
tryJson([self viewHierarchyFromView:window intoContext:&JSONContext]);
88+
tryJson([self viewHierarchyFromView:window
89+
intoContext:&JSONContext
90+
reportAccessibilityIdentifier:reportAccessibilityIdentifier]);
11791
}
11892

11993
tryJson(sentrycrashjson_endContainer(&JSONContext));
@@ -131,7 +105,9 @@ - (BOOL)processViewHierarchy:(NSArray<UIView *> *)windows
131105
return YES;
132106
}
133107

134-
- (int)viewHierarchyFromView:(UIView *)view intoContext:(SentryCrashJSONEncodeContext *)context
108+
+ (int)viewHierarchyFromView:(UIView *)view
109+
intoContext:(SentryCrashJSONEncodeContext *)context
110+
reportAccessibilityIdentifier:(BOOL)reportAccessibilityIdentifier
135111
{
136112
SENTRY_LOG_DEBUG(@"Processing view hierarchy of view: %@", view);
137113

@@ -141,7 +117,7 @@ - (int)viewHierarchyFromView:(UIView *)view intoContext:(SentryCrashJSONEncodeCo
141117
tryJson(sentrycrashjson_addStringElement(
142118
context, "type", viewClassName, SentryCrashJSON_SIZE_AUTOMATIC));
143119

144-
if (self.reportAccessibilityIdentifier && view.accessibilityIdentifier
120+
if (reportAccessibilityIdentifier && view.accessibilityIdentifier
145121
&& view.accessibilityIdentifier.length != 0) {
146122
tryJson(sentrycrashjson_addStringElement(context, "identifier",
147123
view.accessibilityIdentifier.UTF8String, SentryCrashJSON_SIZE_AUTOMATIC));
@@ -166,7 +142,9 @@ - (int)viewHierarchyFromView:(UIView *)view intoContext:(SentryCrashJSONEncodeCo
166142

167143
tryJson(sentrycrashjson_beginArray(context, "children"));
168144
for (UIView *child in view.subviews) {
169-
tryJson([self viewHierarchyFromView:child intoContext:context]);
145+
tryJson([self viewHierarchyFromView:child
146+
intoContext:context
147+
reportAccessibilityIdentifier:reportAccessibilityIdentifier]);
170148
}
171149
tryJson(sentrycrashjson_endContainer(context));
172150
tryJson(sentrycrashjson_endContainer(context));

Sources/Sentry/include/SentryPrivate.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#import "SentrySDK+Private.h"
1717
#import "SentryTime.h"
1818
#import "SentryUserAccess.h"
19+
#import "SentryViewHierarchyProviderHelper.h"
1920
#import "_SentryDispatchQueueWrapperInternal.h"
2021

2122
// Headers that also import SentryDefines should be at the end of this list

Sources/Sentry/include/SentryViewHierarchyProvider.h

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#import "SentryDefines.h"
2+
3+
#if SENTRY_HAS_UIKIT
4+
5+
@class UIWindow;
6+
7+
NS_ASSUME_NONNULL_BEGIN
8+
9+
@interface SentryViewHierarchyProviderHelper : NSObject
10+
11+
/**
12+
* Get the view hierarchy in a json format.
13+
*
14+
* @param windows The app windows.
15+
* @param reportAccessibilityIdentifier Whether or not to report accessibility identifiers.
16+
*/
17+
+ (nullable NSData *)appViewHierarchyFrom:(NSArray<UIWindow *> *)windows
18+
reportAccessibilityIdentifier:(BOOL)reportAccessibilityIdentifier;
19+
20+
/**
21+
* Save the current app view hierarchy in the given file path.
22+
*
23+
* @param filePath The full path where the view hierarchy should be saved.
24+
* @param windows The app windows.
25+
* @param reportAccessibilityIdentifier Whether or not to report accessibility identifiers.
26+
*/
27+
+ (BOOL)saveViewHierarchy:(NSString *)filePath
28+
windows:(NSArray<UIWindow *> *)windows
29+
reportAccessibilityIdentifier:(BOOL)reportAccessibilityIdentifier;
30+
@end
31+
32+
NS_ASSUME_NONNULL_END
33+
#endif
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#if (os(iOS) || os(tvOS) || (swift(>=5.9) && os(visionOS))) && !SENTRY_NO_UIKIT
2+
3+
@_implementationOnly import _SentryPrivate
4+
import UIKit
5+
6+
@_spi(Private) @objc public class SentryViewHierarchyProvider: NSObject {
7+
@objc public init(dispatchQueueWrapper: SentryDispatchQueueWrapper, sentryUIApplication: SentryApplication) {
8+
self.reportAccessibilityIdentifier = true
9+
self.dispatchQueueWrapper = dispatchQueueWrapper
10+
self.sentryUIApplication = sentryUIApplication
11+
}
12+
13+
private let dispatchQueueWrapper: SentryDispatchQueueWrapper
14+
private let sentryUIApplication: SentryApplication
15+
16+
/**
17+
* Whether we should add `accessibilityIdentifier` to the view hierarchy.
18+
*/
19+
@objc public var reportAccessibilityIdentifier: Bool
20+
21+
/**
22+
Get the view hierarchy in a json format.
23+
Always runs in the main thread.
24+
*/
25+
@objc public func appViewHierarchyFromMainThread() -> Data? {
26+
var result: Data?
27+
28+
let fetchViewHierarchy = {
29+
result = self.appViewHierarchy()
30+
}
31+
32+
SentrySDKLog.info("Starting to fetch the view hierarchy from the main thread.")
33+
34+
dispatchQueueWrapper.dispatchSyncOnMainQueue(block: fetchViewHierarchy)
35+
36+
SentrySDKLog.info("Finished fetching the view hierarchy from the main thread.")
37+
38+
return result
39+
}
40+
41+
@objc public func appViewHierarchy() -> Data? {
42+
let windows = sentryUIApplication.getWindows() ?? []
43+
return SentryViewHierarchyProviderHelper.appViewHierarchy(from: windows, reportAccessibilityIdentifier: reportAccessibilityIdentifier)
44+
}
45+
46+
@discardableResult @objc(saveViewHierarchy:) public func saveViewHierarchy(_ filePath: String) -> Bool {
47+
let windows = sentryUIApplication.getWindows() ?? []
48+
return SentryViewHierarchyProviderHelper.saveViewHierarchy(filePath, windows: windows, reportAccessibilityIdentifier: reportAccessibilityIdentifier)
49+
}
50+
}
51+
52+
#endif

0 commit comments

Comments
 (0)