0

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)?

Darshan
  • 2,272
  • 3
  • 31
  • 43
Benjamin B.
  • 521
  • 7
  • 15

1 Answers1

2

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

Calculator_MainView().environmentObject(GlobalEnvironment())
Asperi
  • 228,894
  • 20
  • 464
  • 690
  • so this leads me to an interesting question...if i have an app and an app clip and both start on different views, how can I specify them (since there is only one "let contentView" in the scene delegate)? – Benjamin B. Jul 27 '20 at 05:11
  • and do i need to set the target membership of my appdelegate/scenedelegate to the app clip? Seems like when I do that, parts of my app don't work (cant find firebase and other issues)... – Benjamin B. Jul 27 '20 at 05:16
  • Got this to work! For whomever finds this after me, make sure to check the views that this view is nested into and make sure THOSE views have access to the environment object as well... – Benjamin B. Aug 30 '20 at 19:44