Search code examples
swiftnsdate

enable a button if matches the last date stored +1


I am fairly new to Swift, but getting better.

I have managed to disable a button and store the date. And at this point I have reached the end of my knowledge, so I am hoping someone can help.

The button then needs to be enabled the next day by checking against the date stored, so the user can only use the function once per day.

code is as follows;

import Foundation
import UIKit

class tryForAFiver : UIViewController {


    @IBOutlet weak var drinkImage: UIImageView!
    @IBOutlet weak var redeemButton: UIButton!

    override func viewDidLoad() {
        self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())



    }


    @IBAction func redeemButton(_ sender: Any) {

        let cocktailNumber = arc4random_uniform(32)
        drinkImage.image = UIImage(named: "cocktailList\(cocktailNumber)")

        let userDefaults = UserDefaults.standard

            if var timeList = userDefaults.object(forKey: "timeList") as? [NSDate]
            {
                timeList.append(NSDate())
                userDefaults.set(timeList, forKey: "timeList")
            }
            else
            {
                userDefaults.set([NSDate()], forKey: "timeList")
            }
            userDefaults.synchronize()

        if let timeList = UserDefaults.standard.object(forKey: "timeList") as? [NSDate]
        {
            print(timeList)
        }


       self.redeemButton.isEnabled = false

    }

}

thanks in advance for any help.


Solution

  • I made some changes to your code. Is it OK to use Date() instead of NSDate()? It's easier to work with in Swift.

    Button action:

    @IBAction func redeemButton(_ sender: Any) {
    
        let userDefaults = UserDefaults.standard
    
        if var timeList = userDefaults.object(forKey: "timeList") as? [Date]
        {
            timeList.append(Date())
            userDefaults.set(timeList, forKey: "timeList")
        }
        else
        {
            userDefaults.set([Date()], forKey: "timeList")
        }
        userDefaults.synchronize()
    
        if let timeList = UserDefaults.standard.object(forKey: "timeList") as? [Date]
        {
            print(timeList)
        }
    
        self.redeemButton.isEnabled = false
    }
    

    And on viewDidLoad()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        if let timeList = UserDefaults.standard.object(forKey: "timeList") as? [Date], let lastDay = timeList.last
        {
            if Calendar.current.isDateInToday(lastDay) {
                self.redeemButton.isEnabled = false
            }
            else {
                self.redeemButton.isEnabled = true
            }
        }
    }
    

    This should get you on the right track. A word of warning: neither UserDefaults() nor Date() are safe for doing this kind of thing. Both are easily modified by the client. You should do a server check also if it's important.