Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show a popup only once per day in Swift?

Tags:

swift

I would like to show a popup only once per day in my app and if the user has already seen the popup, then, do not show it again for that day. I was struggling for a while and decided to ask for some help here :)

Update

Here is the code I have wrote so far:

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
}
like image 202
Jkrist Avatar asked Nov 27 '25 10:11

Jkrist


2 Answers

Here is a simple solution. Just store the last Date you've shown the alert and check if that Date falls in today to show or not show the alert.

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()
}
like image 106
Frankenstein Avatar answered Nov 29 '25 03:11

Frankenstein


You can use this as your solution

    override func viewDidLoad() {
    super.viewDidLoad()

    let currentDate = self.DateToString(date: Date(), dateFormatte: "yyyy MM dd")
    
    if self.isKeyPresentInUserDefaults(key: "currentDate") {
        let userDefaluletDate = self.getUserDefaultString("currentDate")
        if userDefaluletDate != currentDate {
            // show Alert
            self.setUserDefaultString(value: currentDate, for: "currentDate")
        }
    }
    else {
        // show Alert
        self.setUserDefaultString(value: currentDate, for: "currentDate")
    }
}

func DateToString(date : Date, dateFormatte : String) -> String {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = dateFormatte
    return (dateFormatter.string(from: date))
    
}

func isKeyPresentInUserDefaults(key: String) -> Bool {
    return UserDefaults.standard.object(forKey: key) != nil
}

func getUserDefaultString(_ forKEY: String)-> String
{
    let defaults = UserDefaults.standard
    return defaults.value(forKey: forKEY) as! String
}

func setUserDefaultString(value: String, for key: String)
{
    let defaults =  UserDefaults.standard
    defaults.set(value, forKey: key)
}
like image 30
Bijender Singh Shekhawat Avatar answered Nov 29 '25 02:11

Bijender Singh Shekhawat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!