gpt4 book ai didi

swift - 知道用户是否更新应用程序或是否有新购买(swift 3)

转载 作者:搜寻专家 更新时间:2023-11-01 07:17:21 25 4
gpt4 key购买 nike

是否有可能知道用户是在安装新购买的产品还是在下载 Swift 中的更新?

我在 Android 和 Obj C 中看到过这个问题,但在 Swift 中没有。

如果有帮助的话,我正在使用 SQLite 数据库。

最佳答案

UserDefaults 中跟踪应用程序版本。我不确定,您为 Objective C 找到了什么,但我敢肯定,它有一些相同的想法。

假设您在 UserDefaults 中的键 appVersion 下跟踪应用程序的版本。比你刚刚阅读它,将它与你当前的应用程序版本进行比较,并检查比较结果。
您可以在 AppDelegate

中执行此操作
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

因此每次您的应用启动时,当前应用版本都会在 UserDefaults 中更新。

Swift 3 示例

let defaults = UserDefaults.standard    
guard let currentAppVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String, let previousVersion = defaults.string(forKey: "appVersion") else {
// Key does not exist in UserDefaults, must be a fresh install
print("fresh install")
// Writing version to UserDefaults for the first time
defaults.set(currentAppVersion, forKey: "appVersion")
return
}

let comparisonResult = currentAppVersion.compare(previousVersion, options: .numeric, range: nil, locale: nil)
switch comparisonResult {
case .orderedSame:
print("same version is running like before")
case .orderedAscending:
print("earlier version is running like before")
case .orderedDescending:
print("older version is running like before")
}

// Updating new version to UserDefaults
defaults.set(currentAppVersion, forKey: "appVersion")

关于swift - 知道用户是否更新应用程序或是否有新购买(swift 3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41414491/

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