OGeek|极客世界-中国程序员成长平台

标题: ios - 在 Today 扩展中加载文件 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 23:49
标题: ios - 在 Today 扩展中加载文件

我一直在尝试创建一个 Today 扩展,它需要访问文档目录中的 .plist 文件。我已经为应用程序和扩展程序设置了应用程序组。

虽然我看到了 NSUserDefaults 的示例,但我找不到任何用于访问文件的内容。

我尝试像这样访问文件(在应用程序本身中有效)

let paths = NSSearchPathForDirectoriesInDomains(
        .DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths[0] as String
let filePath = "\(documentsDirectory)/config.plist"

if NSFileManager.defaultManager().fileExistsAtPath(filePath) // false
{
… 
}

但我在控制台中收到以下错误消息:

Warning: CFFIXED_USER_HOME is not set!  It should be set to the simulated home directory.
Failed to inherit CoreMedia permissions from 59919: (null)

文档说明了有关使用 NSFileCoordinatorNSFilePresenter 的内容。我不知道如何使用它们。我确定我需要在某处告诉他们应用程序组标识符,但我不知道在哪里。



Best Answer-推荐答案


我让它工作了。由于我不需要从扩展编写文件,我认为我不需要使用 NSFileCoordinator。原来 NSFileManager 有一个方法 containerURLForSecurityApplicationGroupIdentifier() 用于检索容器 URL。

let manager = NSFileManager()
let containerURL = manager.containerURLForSecurityApplicationGroupIdentifier("group.MyGroupIdentifier")

let filePath = "\(containerURL.path)/config.plist"
let settings = NSDictionary(contentsOfFile: filePath)

关于ios - 在 Today 扩展中加载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24751028/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4