Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find Apple App Group shared directory

We are currently developing an iOS10 app, including "Messages Extension".

To share CoreDatas persistant store.sqlite inbetween App and Extension, we are using a shared "Apple App Group" directory, which is working fine.

Now we have to get our hands on the store for debug reasons and are unable to find the directory. The Apps container directories are completely empty, which makes sense. But how to download our database? Do we have to somehow copy it programmatically to a reachable place?

To sum it up:

  • We already use CoreData which stores model.sqlite in our shared directory.
  • Everything is up and running.
  • What we want to archive is to download the database to our computer.

Without a shared directory we can simply download the App container from the device, using Xcode->Devices. But as we do use a shared directory, the .sqlite database is not within the container.

Question: How can we download the .sqlite database from the device to our computer?

like image 564
shallowThought Avatar asked Oct 03 '16 17:10

shallowThought


People also ask

What are app groups in iOS?

App groups allow multiple apps produced by a single development team to access shared containers and communicate using interprocess communication (IPC). Apps may belong to one or more app groups. Apps within an app group share access to a group container.


1 Answers

EDIT on 2018-10-12: Updated code for Swift 4.x (Xcode 10). (Older version retained for reference.)

In Swift 4.x:

let sharedContainerURL :URL? = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.etc.etc")
// replace "group.etc.etc" above with your App Group's identifier
NSLog("sharedContainerURL = \(String(describing: sharedContainerURL))")
if let sourceURL :URL = sharedContainerURL?.appendingPathComponent("store.sqlite") {
    if let destinationURL :URL = FileManager().urls(for: .documentDirectory, in: .userDomainMask).first?.appendingPathComponent("copyOfStore.sqlite") {
        try! FileManager().copyItem(at: sourceURL, to: destinationURL)
    }
}

In older version of Swift (probably Swift 2.x):

let sharedContainerURL :NSURL? = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.etc.etc")  // replace "group.etc.etc" with your App Group's identifier
NSLog("sharedContainerURL = \(sharedContainerURL)")
if let sourceURL :NSURL = sharedContainerURL?.URLByAppendingPathComponent("store.sqlite")
{
  if let destinationURL :NSURL = NSFileManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0].URLByAppendingPathComponent("copyOfStore.sqlite")
  {
    try! NSFileManager().copyItemAtURL(sourceURL, toURL: destinationURL)
  }
}

Something like the above will get a file from the app group's shared container to the app's Documents directory. From there, you could use Xcode > Window > Devices to get it to your computer.

You could also use iTunes file sharing to retrieve the file from the app's Documents directory after setting UIFileSharingEnabled to YES in the Info.plist file, but bear in mind that this will expose the directory's contents to the user as well. Should be okay for development/debugging purposes, though.

like image 143
inwit Avatar answered Sep 19 '22 13:09

inwit