5

I have followed this tutorial and added my database. I did target both my app and my extension. From my app I can SELECT, INSERT, DELETE and UPDATE to the database. I want both my app and extension to share the same database. So I add information in my app and then show that information in the extension.

As it works now (I have tested) the app has one instance of the database and the extension has one instance. I have only added one db. Anyone got a clue why I have two instances of my database and why I can´t access the data added from the app in the extension?

DrMickeyLauer
  • 4,455
  • 3
  • 31
  • 67
  • There is no shared server for sqlite. Each instance needs to setup the access on its own. – qwerty_so May 10 '15 at 20:47
  • @ThomasKilian I´m not completely sure that I understood, but both has the access on its own, but is it possible to share the db information between them or not? And if not, is it only NSUserdefaults that you can use if you want to store the information in the phone? –  May 10 '15 at 21:19
  • I'm not an iOS developer (just OSX) so can not tell much about it. But as long as you access the same file you can share the information inside. – qwerty_so May 11 '15 at 04:44

1 Answers1

8

Apps and their extensions are separate processes, so iOS sandboxing normally means they can't touch each other's files. The page you reference doesn't appear to address this in any way.

If you want to use the same SQLite file in both your app and the extension, you need to configure the "app groups" entitlement for both of them. That sets up a new directory that both of them can access, and you put your SQLite file there. You find this directory by using the containerURLForSecurityApplicationGroupIdentifier: method on NSFileManager.

It's not clear to me whether the GitHub project you're using will make use of app groups. There's no other way to share your SQLite file, though. If that project doesn't use app groups, you'll need to either fix it so that it does or stop using it.

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170
  • Were shall I place my custom sqlite file? Don´t really understand it. –  May 11 '15 at 20:49
  • You call `containerURLForSecurityApplicationGroupIdentifier:` to get a path to a directory. You put your SQLite file in that directory. – Tom Harrington May 11 '15 at 20:53
  • I have the path now, but I can´t find the directory. Is there a physical directory that I put the db in or shall I add the db though code? –  May 12 '15 at 16:41
  • You need to use that directory path in code. You can either create a file there from code or copy a file to the directory. It's not available as part of the app install process, so code needs to handle it one way or another. – Tom Harrington May 12 '15 at 17:30