As pointed out in the comments, NSTimer
will only work if your app is currently in the foreground. Code execution for apps that are in the background is quite tricky on iOS.
There are several possibilities to achieve this, which I doubt will fit for your purpose here, examples are:
- Core Location Update: If you are using
Core Location
in your app, your app can receive updates on when the GPS position of the device changes and gets the chance to perform some operations in the background based on the new GPS data
- Voice Over IP: The app provides Voice-over-IP services. Apps with this key are automatically launched after system boot so that the app can reestablish VoIP services. Apps with this key are also allowed to play background audio. (from the Apple Docs)
- Background fetches: With background fetches you can perform network requests on a regular basis, however you are still not able to perform the operations at points in time that you can precisely specify, you can rather tell iOS that you want to perform network requests in regular intervals and iOS will schedule the requests for you. Here is an excellent read on background fetches.
For more information check the Apple Docs on background execution!
For your case, I would recommend you to take a look at UILocalNotification
. It's not 100% clear to me what exactly you are trying to achieve, but UILocalNotification
might be the way to go for you, as it sounds like you want to notify the user based on different times... UILocalNotification
works in the way that you can schedule a notification while the app is in the foreground (or background if you are using these background execution modes) which will notify the user at a specific point in time.
Hope it helps!