gpt4 book ai didi

swift - Inter-App Data Migration(将数据迁移到新的应用程序版本)

转载 作者:搜寻专家 更新时间:2023-10-31 22:17:55 25 4
gpt4 key购买 nike

我目前在 Apple 的 App Store 上有一个 Swift iOS 应用程序。我有很多用户,我想制作一个新版本并帮助当前用户迁移到新版本。仅供引用:新版本是一个 Ionic 应用程序。

在数据方面,我的应用程序使用的是 Core Data,没有任何 iCloud 或同步支持。它包含 JSON 数据以及多个图像。因此,我需要捆绑当前数据并找到一种方法将其引入新的 ionic 应用程序版本。

基本上我的问题是:有没有办法在应用程序的文档目录中写入并让新版本抓取该文件以导入其数据?有没有办法让这两个应用程序传输 AirDrop 或自定义 URL 以外的数据?

我不想远程上传数据,我想在设备上以本地方式无缝地完成所有这些操作,这样用户就不必手动执行任何操作。

欢迎提出建议,谢谢!

最佳答案

我建议使用应用程序组来获取共享容器。我不熟悉 Ionic,但这在原生 Swift 中非常简单。它允许多个应用程序或扩展程序访问一个共享的数据容器,如下图所示:

enter image description here

(图片来自https://agostini.tech/2017/08/13/sharing-data-between-applications-and-extensions-using-app-groups/)


这将需要更新现有应用程序以将数据复制到共享容器,然后用户必须在旧应用程序仍安装时安装新应用程序,因为当没有安装应用程序时共享容器将被删除使用它。

可以这样设置:

1:在项目的“功能”选项卡中启用应用组(针对两个应用)。

2:添加一个新的应用程序组并将其命名为 "group.appDomain.appName" 或类似名称。

3:既然 App Group 已经设置好,它的共享容器可以通过多种方式使用(User Defaults、NSCoding 或 Core Data)。


对于共享用户默认值:

let defaults = UserDefaults.init(suiteName: "group.appDomain.appName")
defaults.set("Example", forKey: "exampleKey")   
defaults.synchronize()

来自 Apple 的更多信息 here .


对于 NSCoding:

let sharedContainerDirectory: URL = FileManager().containerURL(forSecurityApplicationGroupIdentifier: "group.appDomain.appName")!

let sharedArchiveURL: URL = sharedContainerDirectory.appendingPathComponent("whateverYouNeed")

NSKeyedArchiver.archiveRootObject(yourObject, toFile: sharedArchiveURL.path)

对于核心数据:

您可以如下设置容器。我从 this answer 中获取了这段代码因为我自己实际上并没有用 Core Data 尝试过这个。

您使用 containerURL(forSecurityApplicationGroupIdentifier: "group.appDomain.appName")! 使它在共享容器中工作。

lazy var persistentContainer: NSPersistentContainer = {
/*
The persistent container for the application.
This implementation creates and returns a container, having loaded the store for the application to it.
This property is optional since there are legitimate error conditions that could cause the creation of the store to fail.
*/
let container = NSPersistentContainer(name: "xx")

let appName: String = "xx"
var persistentStoreDescriptions: NSPersistentStoreDescription

let storeUrl = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.appDomain.appName")!.appendingPathComponent("xx.sqlite")

let description = NSPersistentStoreDescription()
description.shouldInferMappingModelAutomatically = true
description.shouldMigrateStoreAutomatically = true
description.url = storeUrl

container.persistentStoreDescriptions = [NSPersistentStoreDescription(url: FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.xxx.xx.container")!.appendingPathComponent("xx.sqlite"))]

container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()

This answer提供了一种迁移持久存储的方法。

正如我提到的,我不熟悉 Ionic,所以我不确定在那种情况下工作会如何改变这项技术。

希望对您有所帮助。

关于swift - Inter-App Data Migration(将数据迁移到新的应用程序版本),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50956335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com