Reputation: 2310
I want to run a function in my app at a specific time. At 5pm I'd like my app to display a simple alertView telling the user it's 5pm.
It's safe to assume the app is active and open at that time. I'm aware how to run a function after a delay (but that is dependant on when the method is first called).
One approach is possibly sending a local notification. Is that the best approach? I was hoping to avoid using that as it depends on the user granting permissions to send notifications.
Upvotes: 2
Views: 568
Reputation: 535159
If "it's safe to assume the app is active and open at that time" — though I don't see how it could possibly be safe to assume that — then a timer (NSTimer) is most appropriate. Simply use the difference between what time it is now and the time you want the function called to tell the timer when to fire.
Local notifications are to notify the user about something, when your app is not frontmost. In other words, they are a way of telling the system to provide some notifying interface on your behalf. Notification can appear in the notification center, on the lock screen, as a banner or alert, and as a sound. If that isn't what you want, don't use a local notification.
It's true that if your app happens to be frontmost when a local notification fires, then the local notification notifies your app rather than showing the user a banner / alert. If that's acceptable, then this would be a reasonable use of a local notification. But don't use a local notification as a substitute for doing your own timing arithmetic! Use it because you want to notify the user.
Upvotes: 1