Benjamin B.
Benjamin B.

Reputation: 793

iOS14 App Clip - No ObservableObject of type GlobalEnvironment found

I just added a new app clip to my project and tried to run it on my simulator. Every time I do, I get this error:

Thread 1: Fatal error: No ObservableObject of type GlobalEnvironment found. A View.environmentObject(_:) for GlobalEnvironment may be missing as an ancestor of this view.

I know, this was tackled a bunch of times on here before, but in my project, I have my environment object file target membership set to both my main app and my app clip, and the code for my app clip includes the following:

import SwiftUI

struct Calculator_MainView: View {
    @EnvironmentObject var env: GlobalEnvironment
    
    
    var body: some View {
        if env.calcStyle == 0 {
            ContentView()
        else {
            EmptyView()
            }
    }
}

struct Calculator_MainView_Previews: PreviewProvider {
    static var previews: some View {
        Calculator_MainView().environmentObject(GlobalEnvironment())
    }
}

Not sure what exactly isn't specified right, but when I run this view outside of the app clip (as part of the main app), everything runs fine. Is there a trick to getting environment objects working with app clips (or other targets in general)?

Upvotes: 0

Views: 170

Answers (1)

Asperi
Asperi

Reputation: 257711

Find all places where Calculator_MainView() is created (most probably SceneDelegate.swift) and do the same as in PreviewProvider

Calculator_MainView().environmentObject(GlobalEnvironment())

Upvotes: 2

Related Questions