如何在iOS上的共享应用程序组中保存文件并从中读取

如何在iOS上的共享应用程序组中保存文件并从中读取,ios,swift3,ios10,ios-app-group,Ios,Swift3,Ios10,Ios App Group,我正在将主应用程序中的数据保存到一个文件中,该文件存储在userDomainMask中的某个位置 NSKeyedArchiver.archiveRootObject(stops, toFile: BusStop.ArchiveURL.path) 这在我的Bustop类文件中 static let DocumentsDirectory = FileManager().urls(for: .documentDirectory, in: .userDomainMask).first! static

我正在将主应用程序中的数据保存到一个文件中,该文件存储在userDomainMask中的某个位置

NSKeyedArchiver.archiveRootObject(stops, toFile: BusStop.ArchiveURL.path)
这在我的Bustop类文件中

static let DocumentsDirectory = FileManager().urls(for: .documentDirectory, in: .userDomainMask).first!
static let ArchiveURL = DocumentsDirectory.appendingPathComponent("stops")
现在我想从我的Today扩展及其返回的空集[]中读取这些数据,这样我就知道问题出在哪里了。我需要将此数据保存在我的扩展和应用所属的共享应用组中

如何保存数据并从中读取

我想保存BusStop类的实例数组。 编辑:

我解决了我的问题

使用此解决方案:

static let ArchiveURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.asd.asd.asd")!
    .appendingPathComponent("mypath")
每次对已保存的数据执行某些操作或保存新内容时,请使用此解决方案:


干杯

在Apple Developer Center中设置应用程序组并启用权限()

然后获取应用程序组URL:

guard let groupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: applicationGroupId) else {
    fatalError("could not get shared app group directory.")
}

并使用它将常见文件存储在中。

您将文件存储在文档目录中了吗?@KKRocks是的,它是静态的,让DocumentsDirectory=FileManager().url(用于:.documentDirectory,在:.userDomainMask中)。首先!为什么要使用archiveRootObject进行此操作?@KKRocks此代码来自apple教程中的saveFeeds()方法,添加以下代码行:如何实现它?您的意思是什么?将要共享的所有内容保存到
groupURL
。您缺少哪个实现部分?