UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Check if a week has passed

Forums > SwiftUI

Hi, I would like to run a function every time a week has passed. The first day starts when a user opened the app for the first time ever, and then run it every 7 days. I want the function to run when they open the app, not when the app is closed. (If possible) and also it needs to run once in that day.. So if they opened the app on the 7th day it ran the function and if they close the app and open it again on the same day, it will not run the function again.

More details:

The function will recreate a key-value pair in user defaults. I already know how to do that…

How can I achieve this? Thanks!

2      

If you only want this to run when the user launches the app, not in the background, you can just store the date the function last ran in UserDefaults and check it against the current time on each launch. If it's been a week, run your function.

2      

@roosterboy Thanks!

What if I want to run the function every sunday at 00:00 no matter if the app is open or not?

I saw a few ways but none of them is really reliable...

I also saw a way to do it with local notifications but that means I'd have to ask for permission from the user..

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.