SwiftUI crashes because an EnvironmentObject may be missing as an ancestor
A View.environmentObject(_:) for MyModel may be missing as an ancestor of this viewShort answer
A view that declares @EnvironmentObject needs an ancestor view to supply an object of that exact type with environmentObject(_:). Add the modifier above the view that crashes, usually at the app root. If your model uses the Observable macro, supply it with environment(_:) and read it with @Environment instead.
Fix it fast
1Find out which cause you have
Find the view that declares the property, then every place that view is created. The crash is in the path that has no environmentObject call above it.
grep -rn "@EnvironmentObject" --include=*.swift . grep -rn "environmentObject(" --include=*.swift .2Match what you see, then jump to its fix
- 1
The crash happens when the first screen, or one specific screen, opensNo ancestor supplies the objectFix - 2
The app runs, but the Xcode canvas preview or a view created in another hierarchy crashesA preview or a separately built view has no objectFix - 3
The class is marked @Observable and has no ObservableObject conformanceThe model uses Observable, but the view still uses EnvironmentObjectFix
- 1
What does the error look like?
A View.environmentObject(_:) for MyModel may be missing as an ancestor of this view
Cause 1 of 3
No ancestor supplies the object
1Check
The property wrapper only reads. You must set a matching object on an ancestor view with the environmentObject(_:) modifier, and the type must be the same as the declared type.
grep -rn "environmentObject(" --include=*.swift .2Fix
Create the object once, at the root, and pass it down with the modifier.
@main
struct MyApp: App {
@StateObject private var settings = Settings()
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(settings)
}
}
}3Verify
Run the app and open the screen that crashed.
xcodebuild build -scheme MyApp -destination 'generic/platform=iOS Simulator'Confirmed by developer.apple.com
Cause 2 of 3
A preview or a separately built view has no object
1Check
Supplying the object applies to the view's hierarchy only. A preview builds a view on its own, so the root modifier does not reach it.
grep -rn "#Preview\|PreviewProvider" --include=*.swift .2Fix
Add the same modifier to the view inside the preview, and to any view you create outside the main hierarchy.
#Preview {
ProfileView()
.environmentObject(Settings())
}3Verify
Refresh the canvas; the preview renders.
# Xcode: Editor > Canvas > Resume (Option-Command-P)Confirmed by developer.apple.com
Cause 3 of 3
The model uses Observable, but the view still uses EnvironmentObject
1Check
Apple's note says a model that conforms to Observable uses Environment, set with environment(_:) or environment(_:_:), not EnvironmentObject. Mixing the two leaves the lookup empty.
grep -rn "@Observable" --include=*.swift .2Fix
Insert it with environment(_:) and read it with @Environment and the type.
// insert
ContentView().environment(library)
// read
@Environment(Library.self) private var library3Verify
Build and run; the screen opens. If the object could be absent, declare the property optional so SwiftUI gives nil instead of throwing.
@Environment(Library.self) private var library: Library?Confirmed by developer.apple.com
How do I stop it coming back?
- Inject shared models once, at the App or root view, so every screen below has them.
- Give every preview the same environment modifiers the real app has.
- For new code on iOS 17 and later, prefer an Observable model with Environment.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Mobile apps
UserDefaults logs 'Failed to read values in CFPrefsPlistSource' for an app group
Settings saved by the app are not visible to its extension, widget or sibling app that share the group
This log comes from the preferences system when UserDefaults is opened for an app group's suite.
3 causes, each with a fixMobile apps
iOS app crashes: Google Maps SDK must be initialized via provideAPIKey
Google Maps SDK for iOS must be initialized via [GMSServices provideAPIKey:...] prior to use
The Maps SDK throws this when a Maps object is created before GMSServices.provideAPIKey has run.
3 causes, each with a fixMobile apps
UIKit app crashes because it cannot find the storyboard
Could not find a storyboard named 'Main' in bundle NSBundle
UIStoryboard throws this exception when no storyboard file with that name exists in the bundle it searches.
3 causes, each with a fixMobile apps
Firebase on iOS asks for the FCM token before APNs has given one
Be sure to re-retrieve the FCM token once the APNS device token is set.
The Firebase Messaging SDK needs the APNs device token from iOS before it hands out an FCM token.
3 causes, each with a fixMobile apps
A test push from the Firebase console never shows up on the iPhone
didReceiveRegistrationToken prints a token, yet no notification arrives
When a console message is accepted but the iPhone stays silent, the break is between Firebase and APNs or inside the app.
4 causes, each with a fixMobile apps
InstanceID token() no longer works in a Swift Firebase app
Instance ID calls fail to compile after upgrading Firebase to 8.0.0 or later
Firebase removed the Instance ID dependency from Messaging in 8.0.0, so InstanceID.instanceID() calls no longer belong in a current app.
3 causes, each with a fix