计算过去 1 小时内按下的按钮,即使在用户重新启动应用程序后它也应该工作
Count a button press in past 1 hour, it should work even after the user restart the app
我想统计过去 1 小时内 iOS 中的按钮按下事件。
例如,假设我想限制用户在过去 1 小时内按下按钮 10 次,如果用户尝试第 11 次点击,我将禁用它并且我有一些特定消息要显示在我的应用程序中,用户可以当然从任务中删除应用程序并再次打开它但它应该恢复计数并且时间应该是 运行.
这是一个想法(未测试)
import Foundation
extension UserDefaults {
var buttonPressedCount: Int {
get { self.integer(forKey: #function) }
set { self.set(newValue, forKey: #function) }
}
var buttonPressedStartDateForLastHour: Date {
get { Date(timeIntervalSince1970: self.double(forKey: #function)) }
set { self.set(newValue.timeIntervalSince1970, forKey: #function) }
}
}
struct ButtonPressTracker {
struct Limit {
let count: Int
let duration: TimeInterval
}
// Maximum of 10 presses in an hour
static let maxPressLimit = Limit(count: 10, duration: 3600)
static let defaults = UserDefaults.standard
// Call this every time the button is pressed
// Use the returned Bool value to disable the button
static func logButtonPressed() -> Bool {
var count = defaults.buttonPressedCount
let date = Date()
let lastDate = defaults.buttonPressedStartDateForLastHour
let shouldDisableButton: Bool
if date.timeIntervalSince(lastDate) < maxPressLimit.duration {
count += 1
shouldDisableButton = (count > maxPressLimit.count)
}
else {
count = 1
defaults.buttonPressedStartDateForLastHour = date
shouldDisableButton = false
}
defaults.buttonPressedCount = count
return shouldDisableButton
}
}
是的,只需使用 UserDefaults
即可完成。据我所知,计时器应该在应用程序打开时启动,并在应用程序关闭时关闭。所以你可以
使用[=12]将剩余时间(以秒为单位)和按下次数保存为Int
=],它允许基本数据类型被持久化。您可能应该在 ViewWillDisappear()
方法中将数据保存到 UserDefaults
中。
打开应用程序时。您应该在 ViewDidLoad()
中检索存储在 UserDefaults
中的数据,并使用检索到的数据启动 timer/count。
反之亦然。
如果您想要了解 UserDefaults
的好资源,请查看 this
我想统计过去 1 小时内 iOS 中的按钮按下事件。 例如,假设我想限制用户在过去 1 小时内按下按钮 10 次,如果用户尝试第 11 次点击,我将禁用它并且我有一些特定消息要显示在我的应用程序中,用户可以当然从任务中删除应用程序并再次打开它但它应该恢复计数并且时间应该是 运行.
这是一个想法(未测试)
import Foundation
extension UserDefaults {
var buttonPressedCount: Int {
get { self.integer(forKey: #function) }
set { self.set(newValue, forKey: #function) }
}
var buttonPressedStartDateForLastHour: Date {
get { Date(timeIntervalSince1970: self.double(forKey: #function)) }
set { self.set(newValue.timeIntervalSince1970, forKey: #function) }
}
}
struct ButtonPressTracker {
struct Limit {
let count: Int
let duration: TimeInterval
}
// Maximum of 10 presses in an hour
static let maxPressLimit = Limit(count: 10, duration: 3600)
static let defaults = UserDefaults.standard
// Call this every time the button is pressed
// Use the returned Bool value to disable the button
static func logButtonPressed() -> Bool {
var count = defaults.buttonPressedCount
let date = Date()
let lastDate = defaults.buttonPressedStartDateForLastHour
let shouldDisableButton: Bool
if date.timeIntervalSince(lastDate) < maxPressLimit.duration {
count += 1
shouldDisableButton = (count > maxPressLimit.count)
}
else {
count = 1
defaults.buttonPressedStartDateForLastHour = date
shouldDisableButton = false
}
defaults.buttonPressedCount = count
return shouldDisableButton
}
}
是的,只需使用 UserDefaults
即可完成。据我所知,计时器应该在应用程序打开时启动,并在应用程序关闭时关闭。所以你可以
使用[=12]将剩余时间(以秒为单位)和按下次数保存为
Int
=],它允许基本数据类型被持久化。您可能应该在ViewWillDisappear()
方法中将数据保存到UserDefaults
中。打开应用程序时。您应该在
ViewDidLoad()
中检索存储在UserDefaults
中的数据,并使用检索到的数据启动 timer/count。
反之亦然。
如果您想要了解 UserDefaults
的好资源,请查看 this