15

I have successfully shared data between my App and its Today Extension via userDefaults, but I am having trouble when it comes to actual files located in the app's Documents folder.

When I use this code in my app:

var documentsDir:NSString = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String

it returns:

/var/mobile/Containers/Data/Application/296494AA-946C-40E0-8646-F0895E131DCB/Documents/

but in the extension it returns:

/var/mobile/Containers/Data/PluginKitPlugin/AB484342-537A-4CE7-9EF3-F2CAE352C8A3/Documents/

and obviously, the files are not there.

I read somewhere that I should use:

let containerURL = manager.containerURLForSecurityApplicationGroupIdentifier("group.com.company.app")
let filePath = containerURL.path

but it returns:

/private/var/mobile/Containers/Shared/AppGroup/CD7CF610-EB5F-4246-8D30-D7F8BFA6A219

which didn't work either.

How can I get my iOS 8 Today Extension to read its container app's files located in Documents?

Thanks!

Lee Andrew
  • 798
  • 7
  • 28

1 Answers1

17

You can't access the documents folder of the containing app from extension.

But you can create a shared container that can be accessed by the containing app and all its extension.

You are on the right track using containingURLForSecurityApplicationGroupIdentifier. You just have to use that in both the app and the extension. It does not access the documents folder, but another container.

If you are updating an existing app that has been storing data in documents folder, you will need to move the data to the s8-today-extension

Check out this link. http://www.glimsoft.com/06/28/ios-8-today-extension-tutorial/

Abhinash Jain
  • 169
  • 10
honcheng
  • 2,014
  • 13
  • 14