Hi IJohn,
I'm building my contianing application and Xocde Source Editor Extension with it.
The way I figure to share data between the containing application is through 2 ways.
But first, you NEED to enble App Group capabilities for both targets, the containing application and the extension, and use the same group identifier for both.
One way, is using UserDefaults, which could be helpful for simple and easy key/value pair data access.
Though, remeber that Apple strongly recomends to use UserDefaults small key/value pair data sets of primitive types.
You'll need to add the same group identifier as a suite name using the addSuite method prior any I/O.
Here an example snippet:
UserDefaults.standard.addSuite(named: "group.example.com.appname")
That's it 🙂. Npw, you'll be able to save data from containing application and axcess it via Xcode Source Editor Extension using UserDefaults.
---
The other way, I figure is using CoreData. Using similar technique with the app group settings.
Though, this approach is advance and requires extra configuration.
First thing, is to enable App Group same way as I stated in UserDefaults.
Then, You'll need to add your CoreData capability while first creating the containing application so you can take advantage of the Xcode's generated CoreData stack (recommended). Otherwise, you'll need to create your own stack of CoreData wrapper methods.
Also, you'll need a CoreData stack in the Xcode Source Editor Extension as well. Or build a third target as public framework with a CoreData stack public classes and inlcude it in the containing application and Xcode Source Editor Extension.
Then, you'll need to share data model file with both targets in Build Phases > Compile Sources or by selecting the data model file and go to the file inspector pane and check both targets options in the target section, the containing application and the Xcode Source Editor Extension.
Another configuration, is that you'll need to point the CoreData database location to the App Group container directory. And then tell CoreData to use it. Here is an example snippet:
var durl: URL? = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.example.com.appname")
var surl: URL? = durl?.appendingPathComponent(.sqlite)
let container = NSPersistentContainer(name: )
do {
try container.persistentStoreCoordinator.addPersistentStore(ofType: NSSQLiteStoreType,
configurationName: nil,
at: surl!,
options: nil)
}catch{
print(error.localizedDescription)
}
That's it 😎. Now you'll be able to save data from containing application and axcess it via Xcode Source Editor Extension using CoreData.
IMPORTANT:
Please keep in mind that:
- You need to call the loadPersistentStores method after setting up the custom database location to CoreData.
- As straight forward it seems, I'm going to tell you now that, you'll face few errors and will need to try several times. It is not going to be easy with this approach. But after several attemps, you'll make it happend and it will work 😉.