1

I am trying to feed my Widget the information from my class, but for unknown reason for me, the Widget is not able to see and find the Class! here is my class:

import SwiftUI

class AppColor: ObservableObject {
    
    static let shared: AppColor = AppColor()
    @Published var color: Color = Color.red
    
}

here is my ContentView:

import SwiftUI

struct ContentView: View {
    
    @StateObject var appColor: AppColor = AppColor.shared

    var body: some View {

        ZStack {
            
            appColor.color.ignoresSafeArea()
            
            Button(action: {

                if appColor.color == Color.red {
                    
                    appColor.color = Color.yellow
                    
                }
                else {
                    
                    appColor.color = Color.red
                    
                }

            }) {
                Text("change Color")
                    .foregroundColor(Color.black)
                    .bold()
            }
  
        }
        
  
    }
}

and here is my Widget:

struct testWidgetEntryView : View {
    var entry: Provider.Entry
    
    @StateObject var appColor: AppColor = AppColor.shared

    var body: some View {
        
        appColor.color
        
    }
}

and here is my file positions in app:

enter image description here

pawello2222
  • 46,897
  • 22
  • 145
  • 209
ios coder
  • 1
  • 4
  • 31
  • 91

1 Answers1

3

Even though the files may be in the same project, they can belong to different targets.

When you create a widget extension, you also create a new target. If you want to use a specific file in some target, you need to add it to this target.

You can change this from the right side panel: File Inspector -> Target Membership.

Make sure to check the widget target option for the files you want to use in the widget.

enter image description here


Also note that the app and its extensions are different processes. This means that if you change the appColor in the main app, it won't change in the widget.

You check this post for more information:

pawello2222
  • 46,897
  • 22
  • 145
  • 209
  • I read your other post about Widget, like userDefaults or deep link or appGroup, could I say all things we know about Widget could possibly totally change in future? I think the way we are interacting with Widget are kind of strange for me, how difficult is a simple data sharing, back again about what I wanted say, can I say this would change? and all we know could about Widget could be useless with some new update to Widget? and maybe a hold on Widget for people that they really do not need them in their app? – ios coder Feb 03 '21 at 01:20
  • 1
    @swiftPunk IMHO even with some breaking change update, Home Screen Widgets will remain *extensions*. And communication between an app and its extensions can't be done so *easily* (this doesn't apply to widgets only but to extensions in general). For this reason any *significant* change to the way in how an app shares data with a widget is unlikely. – pawello2222 Feb 03 '21 at 19:09