如何在 Swift 中保存增量
How to save an Increment in Swift
我正在设计一个摄影师的应用程序。我添加了应用率 window。它运行良好,但它的增量不是 working.I 为 "After 3 open the window" 编程的。每次我打开应用程序,控制台输出 "run count = 0"。
这是我的问题,我不知道如何解决。
let runIncrementerSetting = "numberOfRuns" // UserDefauls dictionary key where we store number of runs
let minimumRunCount = 3 // Minimum number of runs that we should have until we ask for review
func incrementAppRuns() { // counter for number of runs for the app. You can call this from App Delegate
let usD = UserDefaults()
let runs = getRunCounts() + 1
usD.setValuesForKeys([runIncrementerSetting: runs])
usD.synchronize()
}
func getRunCounts () -> Int { // Reads number of runs from UserDefaults and returns it.
let usD = UserDefaults()
let savedRuns = usD.value(forKey: runIncrementerSetting)
var runs = 0
if (savedRuns != nil) {
runs = savedRuns as! Int
}
print("Run Counts are \(runs)")
return runs
}
func showReview() {
let runs = getRunCounts()
print("Show Review")
if (runs > minimumRunCount) {
if #available(iOS 11.0, *) {
print("Review Requested")
SKStoreReviewController.requestReview()
} else {
// Fallback on earlier versions
}
} else {
print("Runs are not enough to request review!")
}
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
incrementAppRuns()
return true
}
showReview()
您确定 "Every time I open the app" 您实际上是在重新启动该应用程序吗? (杀死应用程序并再次点击应用程序图标)。如果不是,则 didFinishLaunchingWithOptions:
将不会调用,您将不得不在 applicationDidBecomeActive:
中处理此问题。
除此之外,在使用 UserDefaults
时还有两个建议
- 不要使用
value(forKey:
,而是在处理整数值时使用 integer(forKey:
。
- 并且不要打电话给
.synchronize()
。
以下代码运行良好:
func incrementAppRuns() {
let usD = UserDefaults.standard
let runs = getRunCounts() + 1
usD.set(runs, forKey: runIncrementerSetting)
}
func getRunCounts() -> Int {
let usD = UserDefaults.standard
let runs = usD.integer(forKey: runIncrementerSetting)
print("Run Counts are \(runs)")
return runs
}
我正在设计一个摄影师的应用程序。我添加了应用率 window。它运行良好,但它的增量不是 working.I 为 "After 3 open the window" 编程的。每次我打开应用程序,控制台输出 "run count = 0"。 这是我的问题,我不知道如何解决。
let runIncrementerSetting = "numberOfRuns" // UserDefauls dictionary key where we store number of runs
let minimumRunCount = 3 // Minimum number of runs that we should have until we ask for review
func incrementAppRuns() { // counter for number of runs for the app. You can call this from App Delegate
let usD = UserDefaults()
let runs = getRunCounts() + 1
usD.setValuesForKeys([runIncrementerSetting: runs])
usD.synchronize()
}
func getRunCounts () -> Int { // Reads number of runs from UserDefaults and returns it.
let usD = UserDefaults()
let savedRuns = usD.value(forKey: runIncrementerSetting)
var runs = 0
if (savedRuns != nil) {
runs = savedRuns as! Int
}
print("Run Counts are \(runs)")
return runs
}
func showReview() {
let runs = getRunCounts()
print("Show Review")
if (runs > minimumRunCount) {
if #available(iOS 11.0, *) {
print("Review Requested")
SKStoreReviewController.requestReview()
} else {
// Fallback on earlier versions
}
} else {
print("Runs are not enough to request review!")
}
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
incrementAppRuns()
return true
}
showReview()
您确定 "Every time I open the app" 您实际上是在重新启动该应用程序吗? (杀死应用程序并再次点击应用程序图标)。如果不是,则 didFinishLaunchingWithOptions:
将不会调用,您将不得不在 applicationDidBecomeActive:
中处理此问题。
除此之外,在使用 UserDefaults
- 不要使用
value(forKey:
,而是在处理整数值时使用integer(forKey:
。 - 并且不要打电话给
.synchronize()
。
以下代码运行良好:
func incrementAppRuns() {
let usD = UserDefaults.standard
let runs = getRunCounts() + 1
usD.set(runs, forKey: runIncrementerSetting)
}
func getRunCounts() -> Int {
let usD = UserDefaults.standard
let runs = usD.integer(forKey: runIncrementerSetting)
print("Run Counts are \(runs)")
return runs
}