简体   繁体   English

Swift - 如何只在星期一显示视图并且只显示一次

[英]Swift - How to only show view on a Monday and only show it once

I am trying to implement a weekly roundup feature into my iOS fitness app.我正在尝试在我的 iOS 健身应用程序中实现每周综述功能。 I want to display the weekly roundup view every Monday and only display it once to the user, so they are not shown it every time the launch the app.我想每周一显示每周汇总视图,并且只向用户显示一次,因此每次启动应用程序时都不会显示它们。 I have tried checking if it is a Monday which works fine but can not find a way to only show it once.我试过检查它是否是一个工作正常的星期一,但找不到只显示一次的方法。

One possible solution is to store when you've last shown the weekly roundup to the user, perhaps in a place like UserDefaults.一种可能的解决方案是存储您上次向用户显示每周摘要的时间,可能存储在像 UserDefaults 这样的地方。 To display it, you can make sure that both the Monday check that you've already built, and the date range has passed enough time.要显示它,您可以确保您已经构建了星期一检查,并且日期范围已经过了足够的时间。

    let isMonday = true
    var readyToShow = true
    if let date = UserDefaults.standard.object(forKey: "lastShownRoundup") as? Date, date < Date().addingTimeInterval(86400 * -6) {
        readyToShow = false
    }

    if isMonday && readyToShow {
        //show your roundup
        UserDefaults.standard.set(Date(), forKey: "lastShownRoundup")
    }

The time interval code makes sure that at least 6 days has passed since they've last seen it, which should account for viewing at night on one Monday, but in the morning of the next.时间间隔代码确保距离他们最后一次看到它已经过去了至少 6 天,这应该说明在一个星期一的晚上观看,但在下一个星期一的早上观看。 You could also set this number to -1 and it should work fine.您也可以将此数字设置为 -1,它应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM