0

Main Question: How can I have a file in a project extension read both the other files in that extension and the main app files?

Specific Context for this Issue:

I have recently added a widget extension to my app. I created a .swift file to control the majority of the data being displayed, and, to work properly, it needs to be able to be read by other files in the widget extension, as well as read my main app.

[Background Research]
I have read passing data via the UserDefaults or FileManager is best, however, I am not sure it will work in my situation. I would like the widget controller (the file within the widget extension) to directly access whatever data it needs from the main app, without the need to specifically save it when it is updated, such that when all widget timelines are refreshed, the Widget Controller will automatically read the new data.

[My Approachs and Past Trials]
To solve this issue I have tried to configure the widget Controller file to be able to read main app files and be read by other widget extension files, however, my attempts thus far have been unsuccessful. Here are some of the approaches I have tried:

[1. This was how my app was organized at the beginning and where I first noticed the problem of having a file read both extension and main app files]

a. I initially placed the Widget Controller in the widget extension, with its target memberships being my main app, the widget extension, and the intents extension.

b. All structs here were marked with public (though I think this was unnecessary, I will provide all context I can give)

c. It was able to access other files/structs in the widget extension, (all of which shared the widget extension target membership, but NOT the main app!)

[2. I thought maybe it was because the main app (for these examples I was just looking at one file, but it applies to all files in the main app) only shared the main app target membership and not the widget or intents extension with the Widget Controller, so I added those. Doing that, the file in the main app lost all access to other files in the main app]

[3. I thought maybe I could try removing the widget and intents extension from the target membership of the file in the widget extension (I did not expect this to be a long-term solution, I just wanted to see if by doing that it would be able to access the main app)]

a. The file was in the widget extension

b. it only had the main app in target memberships

c. could not access the main app files. (or any of the files in the widget or intents extension)

[4. With that test, I thought the only way it would be able to access the main app was if it was in the main app folder so I moved it there].

a. Widget Controller moved into the main app folder

b. it only had the main app in target memberships

c. was able to access the main app files (yay), but not those in the widget or intents extensions (nor could they access it)

d. Tried adding back the widget and intents extensions to target memberships, and no longer could access the main app, but could access the extensions

Conclusion

It seems regardless of target membership configurations I am unable to have the Widget Controller be able to access both extensions and main app files, so how can I get this file to be able to read both the widget extensions and the main app files?

(I know this was much information so if any more / any clarity is required, message me :) ) (I know this is similar to: Share data between main App and Widget in SwiftUI for iOS 14, however as I specified I am unable to use the solutions posted there (unless there is something I am missing))

Brian.Masse
  • 75
  • 1
  • 6
  • What is your question? – El Tomato Aug 26 '21 at 23:34
  • @ElTomato I have just edited it, sorry, I should have been more clear to begin with – Brian.Masse Aug 27 '21 at 03:47
  • The main way to share files between the extension and the main app is through AppGroup. [You can use `UserDefaults`](https://stackoverflow.com/questions/63922032/share-data-between-main-app-and-widget-in-swiftui-for-ios-14) or save files directly into the AppGroup and read them from there. https://www.avanderlee.com/swift/core-data-app-extension-data-sharing/ – lorem ipsum Aug 27 '21 at 07:07
  • Configure App group for your widget and app . get app group url and store files to url. guard let groupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: applicationGroupId) else { fatalError("could not get shared app group directory.") } – Vivek Aug 27 '21 at 14:31

0 Answers0