Skip to content

Commit

Permalink
Add WindowReader SwiftUI view
Browse files Browse the repository at this point in the history
  • Loading branch information
dcvz committed Dec 24, 2021
1 parent 79a0a1c commit 921d0a1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,24 @@ struct SettingsView: View, DestinationView {
}
```

### Using with SwiftUI Lifecycle

If using with a project that only has a SwiftUI lifecycle, you don't have a SceneDelegate to hook into. For this case we've created a helper `WindowReader` view that will expose the view so you can create your navigation object:

```swift
@main
struct ExampleApp: App {
var body: some Scene {
WindowGroup {
WindowReader { window in
ExampleView()
.environmentObject(Navigation(window: window!))
}
}
}
}
```

## FAQ

#### Can I present a ViewController - i.e. a SFSafariViewController?
Expand Down
30 changes: 30 additions & 0 deletions Sources/XNavigation/SwiftUI/WindowReader.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// WindowReader.swift
// XNavigation
//
// Created by David Chavez on 24.12.21.
// Copyright © 2021 Double Symmetry UG. All rights reserved.
//

import SwiftUI

@frozen public struct WindowReader<Content>: View where Content: View {
private var content: (UIWindow?) -> Content

private var window: UIWindow? {
guard let scene = UIApplication.shared.connectedScenes.first,
let windowSceneDelegate = scene.delegate as? UIWindowSceneDelegate,
let window = windowSceneDelegate.window else {
return nil
}
return window
}

public init(@ViewBuilder content: @escaping (UIWindow?) -> Content) {
self.content = content
}

public var body: some View {
content(window)
}
}

0 comments on commit 921d0a1

Please sign in to comment.