Xcode Source Editor Extension and Containing application

Hi All:


I've got my Xcode editor extension working well. I've built an application that allows the user to set options and save the document to disk if desired.


So how does the extension get the options the user has set? As mentioned right now, the user can save the options to a document, but is that where the options should be saved? Or application preferences perhaps?


I've read a few of the extensions in sample source code and nothing's jumped out as a way to share user settings between the containing application and the extension.


If someone could point me to a URL that I should review, it would be appreciated.


Thanks in advance for your time



John

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:


  1. You need to call the loadPersistentStores method after setting up the custom database location to CoreData.
  2. 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 😉.
Xcode Source Editor Extension and Containing application
 
 
Q