@@ -22,6 +22,9 @@ enum RedactRegionType {
2222 /// Pop the last Pushed region from the drawing context.
2323 /// Used after prossing every child of a view that clip to its bounds.
2424 case clipEnd
25+
26+ /// These regions are redacted first, there is no way to avoid it.
27+ case redactSwiftUI
2528}
2629
2730struct RedactRegion {
@@ -155,7 +158,19 @@ class UIRedactBuilder {
155158 rootFrame: view. frame,
156159 transform: CGAffineTransform . identity)
157160
158- return redactingRegions. reversed ( )
161+ var swiftUIRedact = [ RedactRegion] ( )
162+ var otherRegions = [ RedactRegion] ( )
163+
164+ for region in redactingRegions {
165+ if region. type == . redactSwiftUI {
166+ swiftUIRedact. append ( region)
167+ } else {
168+ otherRegions. append ( region)
169+ }
170+ }
171+
172+ //The swiftUI type needs to appear first in the list so it always get masked
173+ return swiftUIRedact + otherRegions. reversed ( )
159174 }
160175
161176 private func shouldIgnore( view: UIView ) -> Bool {
@@ -187,11 +202,12 @@ class UIRedactBuilder {
187202 let newTransform = concatenateTranform ( transform, with: layer)
188203
189204 let ignore = !forceRedact && shouldIgnore ( view: view)
190- let redact = forceRedact || shouldRedact ( view: view)
205+ let swiftUI = SentryRedactViewHelper . shouldRedactSwiftUI ( view)
206+ let redact = forceRedact || shouldRedact ( view: view) || swiftUI
191207 var enforceRedact = forceRedact
192208
193209 if !ignore && redact {
194- redacting. append ( RedactRegion ( size: layer. bounds. size, transform: newTransform, type: . redact, color: self . color ( for: view) ) )
210+ redacting. append ( RedactRegion ( size: layer. bounds. size, transform: newTransform, type: swiftUI ? . redactSwiftUI : . redact, color: self . color ( for: view) ) )
195211
196212 guard !view. clipsToBounds else { return }
197213 enforceRedact = true
@@ -248,14 +264,22 @@ class UIRedactBuilder {
248264 Indicates whether the view is opaque and will block other view behind it
249265 */
250266 private func isOpaque( _ view: UIView ) -> Bool {
251- return view. alpha == 1 && view. backgroundColor != nil && ( view. backgroundColor? . cgColor. alpha ?? 0 ) == 1
267+ return SentryRedactViewHelper . shouldClipOut ( view ) || ( view. alpha == 1 && view. backgroundColor != nil && ( view. backgroundColor? . cgColor. alpha ?? 0 ) == 1 )
252268 }
253269}
254270
255271@objcMembers
256- class SentryRedactViewHelper : NSObject {
272+ public class SentryRedactViewHelper : NSObject {
257273 private static var associatedRedactObjectHandle : UInt8 = 0
258274 private static var associatedIgnoreObjectHandle : UInt8 = 0
275+ private static var associatedClipOutObjectHandle : UInt8 = 0
276+ private static var associatedSwiftUIRedactObjectHandle : UInt8 = 0
277+
278+ override private init ( ) { }
279+
280+ static func maskView( _ view: UIView ) {
281+ objc_setAssociatedObject ( view, & associatedRedactObjectHandle, true , . OBJC_ASSOCIATION_ASSIGN)
282+ }
259283
260284 static func shouldMaskView( _ view: UIView ) -> Bool {
261285 ( objc_getAssociatedObject ( view, & associatedRedactObjectHandle) as? NSNumber ) ? . boolValue ?? false
@@ -265,13 +289,25 @@ class SentryRedactViewHelper: NSObject {
265289 ( objc_getAssociatedObject ( view, & associatedIgnoreObjectHandle) as? NSNumber ) ? . boolValue ?? false
266290 }
267291
268- static func maskView( _ view: UIView ) {
269- objc_setAssociatedObject ( view, & associatedRedactObjectHandle, true , . OBJC_ASSOCIATION_ASSIGN)
270- }
271-
272292 static func unmaskView( _ view: UIView ) {
273293 objc_setAssociatedObject ( view, & associatedIgnoreObjectHandle, true , . OBJC_ASSOCIATION_ASSIGN)
274294 }
295+
296+ static func shouldClipOut( _ view: UIView ) -> Bool {
297+ ( objc_getAssociatedObject ( view, & associatedClipOutObjectHandle) as? NSNumber ) ? . boolValue ?? false
298+ }
299+
300+ static public func clipOutView( _ view: UIView ) {
301+ objc_setAssociatedObject ( view, & associatedClipOutObjectHandle, true , . OBJC_ASSOCIATION_ASSIGN)
302+ }
303+
304+ static func shouldRedactSwiftUI( _ view: UIView ) -> Bool {
305+ ( objc_getAssociatedObject ( view, & associatedSwiftUIRedactObjectHandle) as? NSNumber ) ? . boolValue ?? false
306+ }
307+
308+ static public func maskSwiftUI( _ view: UIView ) {
309+ objc_setAssociatedObject ( view, & associatedSwiftUIRedactObjectHandle, true , . OBJC_ASSOCIATION_ASSIGN)
310+ }
275311}
276312
277313#endif
0 commit comments