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

时间:2017-04-28 11:46:56

标签: ios swift3 ios10 ios-app-group

我将我的主数据中的数据保存在一个存储在userDomainMask中的文件中。

NSKeyedArchiver.archiveRootObject(stops, toFile: BusStop.ArchiveURL.path)

这是我的BusStop类文件

static let DocumentsDirectory = FileManager().urls(for: .documentDirectory, in: .userDomainMask).first!
static let ArchiveURL = DocumentsDirectory.appendingPathComponent("stops")

现在我想从我的今日扩展及其返回的空集[]中读取这些数据,所以我知道问题出在哪里。我需要将此数据保存在我的Extension和App所属的共享应用程序组中。

如何保存数据并从中读取数据?

我想保存一个BusStop类的实例数组。

编辑:

我解决了我的问题!

使用此解决方案:

static let ArchiveURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.asd.asd.asd")!
    .appendingPathComponent("mypath")

每次对已保存的数据执行某些操作或保存新内容时,请使用此解决方案:

https://stackoverflow.com/a/37162883/3595696

干杯!

1 个答案:

答案 0 :(得分:3)

在Apple开发人员中心设置应用程序组并启用权利(here is how)。

然后获取应用程序组URL:

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

并使用它来存储常用文件。