Load file in Today extension

a 夏天 提交于 2019-12-14 03:42:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!