Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access app Documents folder from Today Extension

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!

like image 591
Lee Andrew Avatar asked Sep 23 '14 03:09

Lee Andrew


People also ask

How do I access the Documents folder on my iPhone?

Tap Browse at the bottom of the screen, then tap an item on the Browse screen. If you don't see the Browse screen, tap Browse again. To view recently opened files, tap Recents at the bottom of the screen. To open a file, location, or folder, tap it.

How do I access app files in iOS?

On your iPhone, iPad, or iPod touch, open the Files app. Don't see the blue Files app icon on your Home Screen? Just swipe down, then search for the Files app. In the Files app, search or browse for the file that you want.

What is document directory in iOS?

Every iOS app gets a slice of storage just for itself, meaning that you can read and write your app's files there without worrying about colliding with other apps. This is called the user's documents directory, and it's exposed both in code (as you'll see in a moment) and also through iTunes file sharing.


1 Answers

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/

like image 99
honcheng Avatar answered Oct 24 '22 23:10

honcheng