问题
I've been trying to create a Today extension that needs access to a .plist
file in the documents directory. I have setup the App group for both the app and the extension.
While I have seen examples for NSUserDefaults
, I couldn't find anything for accessing files.
I tried accessing the file like this (which works in the app itself)
let paths = NSSearchPathForDirectoriesInDomains(
.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths[0] as String
let filePath = "\(documentsDirectory)/config.plist"
if NSFileManager.defaultManager().fileExistsAtPath(filePath) // false
{
…
}
But I get the following error messages in the console:
Warning: CFFIXED_USER_HOME is not set! It should be set to the simulated home directory.
Failed to inherit CoreMedia permissions from 59919: (null)
The documentation says something about using NSFileCoordinator
and NSFilePresenter
. I couldn't find out how to use them though. I'm sure I'll need to tell them the app group identifier somewhere but I don't where.
回答1:
I got it to work. Since I don't need to write the file from the extension, I don't think I need to use NSFileCoordinator
. Turns out NSFileManager
has a method containerURLForSecurityApplicationGroupIdentifier()
for retrieving the container URL.
let manager = NSFileManager()
let containerURL = manager.containerURLForSecurityApplicationGroupIdentifier("group.MyGroupIdentifier")
let filePath = "\(containerURL.path)/config.plist"
let settings = NSDictionary(contentsOfFile: filePath)
来源:https://stackoverflow.com/questions/24751028/load-file-in-today-extension