Search code examples
iosswiftxcodedelaynsuserdefaults

30 Minute Post Delay Not Working With Swift


How do I create my program that will only allow users to post every 30 minutes using swift? This delay time only works if the app is running, besides that it will not work or restart the time running every time the app launches. I need a way to have them wait for only 30 minutes. My code right now is:

DispatchQueue.global(qos: .background) .async {
                DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1800)) {
                    UserDefaults.standard.set(false, forKey: "PostTimeLimit")
                }
            }

Any tips or solutions would be very helpful. Thanks!


Solution

  • Instead of storing whether or not the user is allowed to post, store their last post time and compare it to the current time.