Swift 4:使用日历检查自首次启动以来是否已过一周,一个月,两个月等

时间:2019-04-02 01:25:47

标签: ios swift calendar

我正在尝试添加计时器,以便在应用程序的首次启动日期之后一周,一个月,两个月等出现一个按钮。

我使用过docs(请参见下面的代码),但是对于更长的时间(周/月)而言,它似乎无效。 Swift文档建议使用日历,但是我不确定该怎么做。

override func viewDidLoad() {
       super.viewDidLoad()

        if installedDate == nil {
            installedDate = Date()
            let timer = Calendar.current

            Button1.isHidden = true
            Button2.isHidden = true
            Button3.isHidden = true
        }

        else {
            print("Not first run")
            print(installedDate!)

            let Button1Date = Date().addingTimeInterval(604800)      //for after 1 week
            let Button2Date = Date().addingTimeInterval(2592000)     //for after 1 month
            let Button3Date = Date().addingTimeInterval(5184000)     //for after 2 months


            if installedDate == Button1Date {
                Button1.isHidden = false
            }
            if installedDate == Button2Date {
                Button2.isHidden = false
            }
            if installedDate == Button3Date {
                Button.isHidden = false
            }

        }

    }

    var installedDate: Date? {
        get {
            return UserDefaults.standard.object(forKey: "installedDateKey") as? Date
        }
        set {
            UserDefaults.standard.set(newValue, forKey: "installedDateKey")
        }
    }

比较启动日期和我需要的预期日期的更有效方法是什么?

1 个答案:

答案 0 :(得分:2)

根据我对您的问题的了解,如果他们使用该应用程序的时间超过一定时间,则希望显示一个按钮。如果是这样,我已经写了一些对您有帮助的代码:

let timeSinceInstalled = -(installedDate ?? Date()).timeIntervalSinceNow

Button1.isHidden = timeSinceInstalled < 604800
Button2.isHidden = timeSinceInstalled < 2592000
Button3.isHidden = timeSinceInstalled < 5184000

实际上,您可以替换viewDidLoad()函数内部的大多数代码,甚至包括您检查installedDate == nil的部分。这是您更新的代码:

override func viewDidLoad() {
    super.viewDidLoad()
    let timeSinceInstalled = -(installedDate ?? Date()).timeIntervalSinceNow
    Button1.isHidden = timeSinceInstalled < 604800
    Button2.isHidden = timeSinceInstalled < 2592000
    Button3.isHidden = timeSinceInstalled < 5184000
}

var installedDate: Date? {
    get {
        return UserDefaults.standard.object(forKey: "installedDateKey") as? Date
    }
    set {
        UserDefaults.standard.set(newValue, forKey: "installedDateKey")
    }
}

如果要在按钮不可单击时将标题设置为“没有可用的标题”,则代码如下:

override func viewDidLoad() {
    super.viewDidLoad()
    let timeSinceInstalled = -(installedDate ?? Date()).timeIntervalSinceNow
    setTitle(Button1, timeSinceInstalled < 604800)
    setTitle(Button2, timeSinceInstalled < 2592000)
    setTitle(Button3, timeSinceInstalled < 5184000)
}

func setTitle(_ button: UIButton, _ statement: Bool) {
    button.setTitle(statement ? "You have none available" : "Click me", for: .normal)
    button.isEnabled = !statement
}

var installedDate: Date? {
    get {
        return UserDefaults.standard.object(forKey: "installedDateKey") as? Date
    }
    set {
        UserDefaults.standard.set(newValue, forKey: "installedDateKey")
    }
}