在Today扩展中加载文件 [英] Load file in Today extension

查看:228
本文介绍了在Today扩展中加载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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)

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

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.

推荐答案

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

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)

这篇关于在Today扩展中加载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆