Skip to content

Instantly share code, notes, and snippets.

@pookjw
Created July 6, 2024 15:01
Show Gist options
  • Save pookjw/50b911422eac5d935f3971bb02cc3927 to your computer and use it in GitHub Desktop.
Save pookjw/50b911422eac5d935f3971bb02cc3927 to your computer and use it in GitHub Desktop.
import SwiftUI
import RegexBuilder
struct ContentView: View {
@Environment(\.windowScene) private var windowScene
var body: some View {
Button("Print UIWindowScene") {
if let windowScene {
print(windowScene)
}
}
}
}
extension EnvironmentValues {
var windowScene: UIWindowScene? {
guard
let _plist: Any = Mirror(reflecting: self).descendant("_plist"),
let description: String = (_plist as? CustomStringConvertible)?.description
else {
return nil
}
let regex = Regex {
"scene = <UIWindowScene: 0x"
Capture {
OneOrMore(.hexDigit)
}
">;"
}
guard let output = description.firstMatch(of: regex)?.1,
let attr = Int(output, radix: 16)
else {
return nil
}
return unsafeBitCast(
UnsafeRawPointer(bitPattern: attr), to: UIWindowScene.self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment