作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在我的应用程序中每天仅显示一次弹出窗口,如果用户已经看到该弹出窗口,则当天不再显示它。我挣扎了一会儿,决定在这里寻求帮助:)
更新
这是我到目前为止编写的代码:
func showPopupOncePerDay() -> Bool {
let lastPopup = UserDefaults.standard.double(forKey: "lastPopup")
let lastPopupDate = Date(timeIntervalSinceNow: lastPopup)
let lastPopupIsToday = NSCalendar.current.isDateInToday(lastPopupDate)
if !lastPopupIsToday {
navigator.showAlertPopup()
}
UserDefaults.standard.set(Date().timeIntervalSince1970, forKey: "lastPopup")
return true
}
最佳答案
这是一个简单的解决方案。只需存储您显示警报的最后一个 Date
并检查该 Date
是否在今天以显示或不显示警报。
let lastAlertDateKey = "lastAlertDate"
func checkIfAlertShownToday() {
if let lastAlertDate = UserDefaults.standard.object(forKey: lastAlertDateKey) as? Date {
if Calendar.current.isDateInToday(lastAlertDate) {
print("Alert was shown today!")
} else {
showAlert()
}
} else {
showAlert()
}
}
func showAlert() {
print("Need to show an alert today!")
UserDefaults.standard.set(Date(), forKey: lastAlertDateKey)
navigator.showAlertPopup()
}
关于swift - 如何在 Swift 中每天只显示一次弹出窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63188447/
我是一名优秀的程序员,十分优秀!