Created
July 6, 2024 15:01
-
-
Save pookjw/50b911422eac5d935f3971bb02cc3927 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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