Perform an action daily at a specified time - ios

In my application I add income and expense data and display it in a home page as a summary. Now suppose I have some fixed expense, which happens daily, that will be automatically added to the expenses at the time which I set. How can I do this?

Use UILocalNotification with repeatInterval property which reschedule the notification with calendar interval. I suggest to go through following apple iOS guideline.
UILocalNotification
Hope, this will help you..

In my opinion, you should use timer or notification only when you are creating a reminder kind of feature.
As per my understanding from your question, I would suggest you following suggestion:
Keep track of last time that the expense was added to your app's local database. You can store this information in database or in user defaults.
On next run of the application, check last entry time and then calculate other entries that needs to be done. If it is daily, it will be the number of days since last entry.
Add the required entries to your database to make your entries consistent.

Related

iOS do scheduled operation in background or when app active

I have CoreData model which I want to update at 12 AM. So it's kind of an even when the app can recognize that a new day is coming and at 12 AM change some things in data models.
Initially, the idea was:
Prepare a single function that returns updated data. So before the function returns smth I every time check for the time (NSDate interval between two dates) and then update data model (if it's a new day). But the architecture not so simple for this purpose and it will take some time to prepare for a single point where I can get updated data, also it takes some time in background to update CoreData model which also adds some expenses to this task.
Is this ok solution to use some timer which will update data at 12 AM, I don't care about consistency in this case, but I don't like a timer which is checking every single second is 12 AM already or not. Is there some push notification update or some scheduler manager in iOS which can update data for me. One more time I just want to update the data layer and I don't care about consistency in UI. If consistency matter for sure then I would like to follow initial ide with a single point of retrieving data.
So I probably need some scheduler manager for this purpose or rewrite code of how I get the data.
There is no way to execute a function at regular intervals, even when the app is backgrounded/killed by the user.
The most reliable solution for executing a function at regular intervals even when the app is backgrounded is to use push notifications scheduled for the specific time intervals (midnight each day in your case), which would wake up the app and let it update its data. However, this solution has its downsides, since you need a server to send the push notification from and the users device needs to be connected to the internet. Also, push notifications don't wake up the app in case the user manually killed it.
For your particular problem, the best solution would be to refactor your code in a way that you have a single function that can be used to retrieve data and hence this function could ensure the data is updated in case a certain time interval has passed since the last update.
You might want to look into BGProcessingTask. You won't have granular control over when you're granted CPU time but you can set the interval you'd prefer that the task execute. Ultimately, when you run and how often is up to the system.
I would recommend checking out the new BackgroundTasks Framework Apple releases for iOS 13.
https://developer.apple.com/documentation/backgroundtasks
While you are not guaranteed at specific time if you have a window (12 am - 3 am) scheduling background tasks may be sufficient for you.

Local Notifications limit for Calendar app

Im building a Calendar app, where you can schedule events, and you get Notified by UserNotification.
The Problem is that i recently read that you can only have 64 scheduled events. But what if the user has more than 64 events? I know repeated notifications are counted as one.
Does this limit count for all notification types (Timer,Calendar,Loacation)?
How would u solve this issue? Since i don't use a server, i cant make push/remote notifications.
Looking forward to ur answers!
Thanks in advance!
After the 64th event, you can try to save the ones after that and schedule them once the number of current scheduled event is less than 64. You should take into account the event's schedule time to avoid missing event with sooner start date.
EDIT
Since your app allows user to schedule future events, it makes sense to use CoreData to persist data. For each event they created, you can create an entity with following attributes:
event name
event start date
isScheduled boolean
This should be fairly simple. After that, whenever the app starts, you can fetch the events and schedule the ones with closest start date. This way, you don't have to schedule an event too far ahead.
If you want to check the number of scheduled events, you can do
UIApplicaiton.shared.scheduledLocalNotifications?.count
This method works but it is deprecated so you might want to use
UNUserNotificationCenter.getPendingNotificationRequests

Is it possible to force the device's date to test local notifications?

I am creating a UILocalNotification with a particular date.
I want to test the reminders that I set for different days. I also want to see if the repetition feature works. Is it possible to force the current time to different date and time so I can see whether it works?
Well one way that I used to do it is to actually change the time and date on my iPhone itself through the Settings app.
If you don't know where to do this, here's a wikihow article.
If you pass the time/date when the notification is supposed to go off, your iPhone will automatically make it go off.
As David pointed out, you have to check by changing the date and time on your phone. But I would also like to point out something.
The default timeZone() of a notification is set according to your location. So even if you manually change the device date and time, the notification will not fire up. For it to work properly you need to set the timeZone() to systemTimeZone(). You can try this:
notification.timeZone = NSTimeZone.systemTimeZone()
Hope this helps. :)
One thing that I have noticed, is that testing local notifications is impossible by setting the date forward to the exact date and time they need to fire. The way that I've approach this is by setting the time to one minute prior to the expected time that it needs to fire, and waiting out the 60 seconds to see it appear. Hope this advice helps :)
Disable automatically time in setting
Run code add local notification(comp.timeZone = TimeZone.current)
Change time in setting to a time before your notification fire time
Wait until the fire time

Can a Passbook pass be relevant for multiple days?

I'm trying to use relevantDate to refine when the passes for my app are shown.
The relevantDate options I know about are: specify a start and end time on a single day, or specify a single time which seems to show the pass in the lock screen for about a four-hour window in either direction (!), at least for the "generic" pass type.
It seems like there ought to be a way to specify, e.g., for a coupon, that it should be shown on the lock screen when they're in the store for the next two weeks, at which point it should expire. Is there? If so, what is it?
Sorry, there's not currently a way to do this.
Relevance is a cooperative effort between you and the system. Your pass gives a point in time (the relevant date) and/or a point in space (the relevant locations). There's no API to provide a duration or a region. The system decides what radius to use around that location and what window of time to use around the date. There is some documentation for the relevant locations in the Passbook guide but the time window is not documented. In practice, the time window is on the order of 4-8 hours, depending on the pass style.
You should go on Apple's bug report page and file an enhancement request, describing how it would add value to your coupons to be relevant for multiple days.
Although not quite what you are looking for, you could send a push update to remove the locations after the promotion period ends.
If you have a promotion that is valid in a particular store, then you could use locations with relevantText appropriate for the promotion period E.g. 20% Off, Ends Jan 20. Once the promotion ends, you then send a push with no locations (or replace with a new offer).
The relevantDate key is not supported by the coupon or storeCard pass types and there is no way to specify a custom lock screen message for a time based alert, so personally I prefer to use location alerts whenever a location is known. The exception would be when it makes sense to remind the user a few hours beforehand (E.g. for a dental appointment or scheduled personal training session).

Auto-detect time of day

I'm writing a daily to do list app, and want the app to update certain elements with each new day. This isn't a problem if the user closes the app one day and then reopens it the next -- I just compare days in the startup methods.
However, this is a problem if the user happens to be using the app exactly at midnight. At midnight, daily task elements need to reset themselves automatically for the new day.
Here's my issue -- I could have a constant thread running in the background, always accessing [NSDate date] and checking for a new day. But I feel like there must be a better way, especially because this only has to happen once per day (and then, only if the user is using the app at midnight).
Thank you in advance!
In applicationDidFinishLaunching: and applicationWillEnterForeground:, setup an NSTimer scheduled to trigger at midnight. Also, register for UIApplicationSignificantTimeChangeNotification to reschedule the timer for time zone changes etc.

Resources