@Volker88 is totally right, you have to add @Environment(\.scenePhase) var scenePhase. So the full example should be:
import SwiftUI
@main
struct TestApp: App {
@Environment(\.scenePhase) var scenePhase
var body: some Scene {
WindowGroup {
ContentView()
}
.onChange(of: scenePhase) { phase in
switch phase {
case .active:
print("Active")
case .inactive:
print("Inactive")
case .background:
print("Background")
@unknown default:
print("Unknown")
}
}
}
}
Post
Replies
Boosts
Views
Activity
Same here on Xcode 12.0 beta 5 (12A8189h) with my App PDF Archiver - https://github.com/PDF-Archiver/PDF-Archive-Viewer/tree/ios-14.
@crystalminds in your example the app only shows 2 colums on every sidebar item, which is not the intended behavior. It should show the sidebar + 2 columns on sidebar item one and two. On sidebar item 3 there should only be the sidebar + 1 column.
I have filed a radar/feedback for this and included all the information in this repository:
https://github.com/JulianKahnert/NavigationExample
Hi @JoeKun,
thx for your response! Awesome to see the „live progress“ of your work… ☺️
Best regards
Julian
Hi,
I encountered the same issue!
Steps to reproduce the crash:
Environment: iOS 17.5.1
Use a template project with SwiftData using CloudKit (see Code 1)
Install it via Testflight on two devices (A & B)
Add items in the CloudKit container (on a regular basis) via Device A
Download the App via Testflight on Device B and move the app after a few seconds to the background (not force closing the app)
Crash after some time
Fix: Move the .modelContainer(for: [Item.self]) ViewModifier from the WindowGroup to the ContentView.
Assumption:
The problem occurs when the App starts in the Background after a notification was received.
Maybe the WindowGroup behaves differently when started in the background, because no window exists!?
I could only reproduce it via a prod CloudKit environment
@DTS Engineer if that's the case, maybe the template projects in Xcode should be updated, so new projects do not trigger that crash.
Code 1 Crashing examaple:
import SwiftData
import SwiftUI
@main
struct ExampleApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
.modelContainer(for: [Item.self])
}
}
Code 2 Fixed with:
import SwiftData
import SwiftUI
@main
struct ExampleApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.modelContainer(for: [Item.self])
}
}
}
Hi,
just a cross reference to my answer on this thread.
tl;dr: I could solve it by moving the view modifier from the WindowGroup to the ContentView. I have not tested it for the .environment view modifier.
Non crashing example:
import SwiftData
import SwiftUI
@main
struct ExampleApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.modelContainer(for: [Item.self])
}
}
}