Skip to content

Commit c634ef8

Browse files
authored
Fixed compilation when building with APPLICATION_EXTENSION_API_ONLY (#352)
1 parent 5c3d214 commit c634ef8

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

Sources/SnapshotTesting/Common/View.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,18 @@ private final class NavigationDelegate: NSObject, WKNavigationDelegate {
664664
#endif
665665

666666
#if os(iOS) || os(tvOS)
667+
extension UIApplication {
668+
static var sharedIfAvailable: UIApplication? {
669+
let sharedSelector = NSSelectorFromString("shared")
670+
guard UIApplication.responds(to: sharedSelector) else {
671+
return nil
672+
}
673+
674+
let shared = UIApplication.perform(sharedSelector)
675+
return shared?.takeUnretainedValue() as! UIApplication?
676+
}
677+
}
678+
667679
func prepareView(
668680
config: ViewImageConfig,
669681
drawHierarchyInKeyWindow: Bool,
@@ -680,7 +692,7 @@ func prepareView(
680692
let traits = UITraitCollection(traitsFrom: [config.traits, traits])
681693
let window: UIWindow
682694
if drawHierarchyInKeyWindow {
683-
guard let keyWindow = UIApplication.shared.keyWindow else {
695+
guard let keyWindow = UIApplication.sharedIfAvailable?.keyWindow else {
684696
fatalError("'drawHierarchyInKeyWindow' requires tests to be run in a host application")
685697
}
686698
window = keyWindow

0 commit comments

Comments
 (0)