Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Fixes

- Fix UIWindow being possibly accessed from a background thread in SentryCrashWrapper (#6905)

## 9.0.0-rc.1

### Breaking Changes
Expand Down
5 changes: 5 additions & 0 deletions Sources/Sentry/SentryDependencyContainerSwiftHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ @implementation SentryDependencyContainerSwiftHelper
return [SentryDependencyContainer.sharedInstance.application getWindows];
}

+ (CGSize)activeScreenSize
{
return [SentryDependencyContainer.sharedInstance.application getActiveWindowSize];
}

#endif // SENTRY_HAS_UIKIT

+ (NSString *)release:(SentryOptions *)options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ NS_ASSUME_NONNULL_BEGIN
#if SENTRY_HAS_UIKIT

+ (nullable NSArray<UIWindow *> *)windows;
+ (CGSize)activeScreenSize;

#endif // SENTRY_HAS_UIKIT

Expand Down
2 changes: 2 additions & 0 deletions Sources/Swift/Helper/SentryApplication.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import UIKit
*/
func getWindows() -> [UIWindow]?

func getActiveWindowSize() -> CGSize

var connectedScenes: Set<UIScene> { get }

var delegate: UIApplicationDelegate? { get }
Expand Down
17 changes: 17 additions & 0 deletions Sources/Swift/Helper/SentryApplicationExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import UIKit
internal_getWindows()
}

@objc public func getActiveWindowSize() -> CGSize {
internal_getActiveWindowSize()
}

@objc public func relevantViewControllersNames() -> [String]? {
internal_relevantViewControllersNames()
}
Expand Down Expand Up @@ -55,6 +59,19 @@ extension SentryApplication {
return Array(windows)
}

public func internal_getActiveWindowSize() -> CGSize {
var size = CGSize.zero
Dependencies.dispatchQueueWrapper.dispatchSyncOnMainQueue({ [weak self] in
guard let self,
let window = self.internal_getWindows()?.first else {
return
}

size = window.bounds.size
}, timeout: 0.01)
return size
}

// This cannot be declared with @objc so until we delete more ObjC code it needs a separate
// function than the objc visible one.
public func internal_relevantViewControllersNames() -> [String]? {
Expand Down
8 changes: 4 additions & 4 deletions Sources/Swift/SentryCrash/SentryCrashWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,10 @@ public final class SentryCrashWrapper: NSObject {
private func setScreenDimensions(_ deviceData: inout [String: Any]) {
// The UIWindowScene is unavailable on visionOS
#if (os(iOS) || os(tvOS)) && !SENTRY_NO_UIKIT
if let appWindows = SentryDependencyContainerSwiftHelper.windows(),
let appScreen = appWindows.first?.screen {
deviceData["screen_height_pixels"] = appScreen.bounds.size.height
deviceData["screen_width_pixels"] = appScreen.bounds.size.width
let screenSize = SentryDependencyContainerSwiftHelper.activeScreenSize()
if screenSize != CGSize.zero {
deviceData["screen_height_pixels"] = screenSize.height
deviceData["screen_width_pixels"] = screenSize.width
}
#endif // (os(iOS) || os(tvOS)) && !SENTRY_NO_UIKIT
}
Expand Down
4 changes: 4 additions & 0 deletions Tests/SentryTests/TestSentryUIApplication.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ final class TestSentryUIApplication: SentryApplication {
return internal_getWindows()
}

func getActiveWindowSize() -> CGSize {
return internal_getActiveWindowSize()
}

private var _windows: [UIWindow]?
private(set) var calledOnMainThread = true
var windows: [UIWindow]? {
Expand Down
Loading