So like I want to allow my app to check if one week has passed, and if that is true, Increase a certain variable, while at the same time, checking if the user has opened the app in that one week. If the user hasn't opened the app in the one week that passed, the app sends a notification to the user to open the app.
I've thought of using NSTImer to do this but I'm worried that the timer would stop when the user closes the app
Is there a way for an iOS app to keep track of real-time without using NSTimer?
These are different issues:
if one week has passed, and if that is true, Increase a certain variable
Store a UserDefault with the starting date. When you launch, check the value. And observe significantTimeChangeNotification to see date changes while the app is running. In either case, if a week has passed, update your variable and the UserDefault.
If the user hasn't opened the app in the one week that passed, the app sends a notification to the user to open the app.
Completely separate issue. This is done by scheduling a LocalNotification periodically for 1 week in the future. Periodically (i.e. during app launch and significantTimeChangeNotification) reschedule it. When it fires, it will show your alert.
Related
I'm currently working on a simple app that displays data received over the network in a watchOS complication. Notably, this data is only relevant for ~30 minutes before a new network fetch is required.
I'd like to have the complication be up to date when the user unlocks their watch in the morning (this is a common use case presented by Apple).
Is it possible to receive some kind of background task when the user unlocks their watch? If I schedule a background task and the watch is locked and charging when the refresh happens, will the background task still fire? What techniques can I use to have data ready for the user when they wake up and unlock their watch? Is there documentation specifically focusing on background tasks when the watch is locked?
As far as I know, the watch works in its locked state only slightly differently from its unlock state.
One difference is the display of complications:
You can specify the privacy behaviour, i.e. what the clock face displays as complication (you can select what is displayed on the lock screen).
So, to my mind, it is possible to run background tasks as scheduled when the watch is locked and charging. Thus the data will automatically be ready when the watch is unlocked.
For this reason, there is no special documentation what happens when the watch is locked, except for some special cases, as what is displayed on the watch face in the locked state.
I would use the app life cycle docs here, and quite possibly choose:
in applicationDidEnterBackground(), I'd set a flag (time when the
complication was last updated). I'd suggest you use a singleton, so that it's accessible anywhere in your app.
then in applicationDidBecomeActive() i'd pick up the flag, compare it
with the current time, and notify the active ViewController to
refresh its data, if it's greater than 30 minutes.
if the flag doesn't exist, because the app was terminated, or it's a
first launch, then refresh anyway (set a 24h date in the past, to
use the same logic as in 1/,2/)
if you want to make it more permanent, use NSDefaults to store the last time the complication was updated.
I'm working on an application notifying Muslims at prayer time. Every day, five different notifications need to be set, notifying the users it's time to pray.
I was able to schedule notifications for one day, but how can I set them every day again? The 5 times need to be changed every day without user interaction or having to open the app.
Is there some kind of callback when the last notification fires, so I can setup notifications for the next day?
One way is to schedule notifications for a week (5*7) if you can get their times correctly , on the basis of that the user may open your app at least once/week
Other way is to make use of background fetch capability where IOS wakes your app to run at any time On the assumption that you re-schedule them for a week from the launch time
I am building an app that requires core date to update every Sunday. e.g. it will save "thisWeek"'s data to "lastWeek" every beginning of a new week, and reset "thisWeek" to let user input new data for new week.
My question is how to achieve this even if user is not running the app at all?
Use UILocalNotification with repeatInterval property which reschedule the notification with calendar interval. I suggest to go through following Apple iOS guidelines. Bottom line, your app will be open when notification will occur.
UILocalNotification
Just do the archiving if necessary whenever the user starts the app. If your data is timestamped this should be simple and there is no requirement for the phone to even be switched on.
This isn't possible. Your application cannot execute code unless the user has launched it. You could provide a local notification to prompt the user to reopen the app, however this is a sub-optimal solution.
A better solution would be to timestamp your data and then whenever the user opens the app, regardless of when, you can process all of the data and move it where it needs to go.
I would show an alert to the user when the user updates the app (not at the first installation) and/or show an alert to the user after the seven days app usage for example.
Is it possible? Where can I begin from?
Thanks.
For an alert when the app updates: store the app version in NSUserDefaults. On launch (use -applicationDidFinishLaunching:), check this value. If it's different from the current version, throw up an alert then update the value. If the value is empty then it's first launch.
For after seven days, do something similar: store the date of first launch in NSUserDefaults. On each launch, check if that is more than seven days ago. You might want to also store a boolean to show whether you've done this alert, as otherwise you'll be nagging users on every launch beyond seven days. You might also want to do the check in -applicationDidEnterForeground: as well.
I am developing an enterprise app which has a lot of dynamic content. Is there a way to have the app auto update the content(new stories, download new videos, etc) at 3am every Sunday.
Is this possible?
While it’s not possible to do this when the app isn’t running, you can do it fairly easily at launch or while running (if it’s going to be running at 3 AM). Here’s what I’d do:
Store an NSDate using NSUserDefaults for the last time you updated.
At launch, if a 3 AM period has passed since that date, initiate a sync.
Also at launch, start an NSTimer with a long interval—5 minutes or so. At each firing, check if a 3 AM period has passed and if it has, initiate a sync. You could even roll the last bullet’s code into this NSTimer’s firing method and just run it once at launch. Just be sure to update the NSDate object each time.
In the application delegate, in the methods called from returning from the background, check the time and sync if necessary—or just start the NSTimer and have it fire immediately first.
That should cover all of the scenarios where you need to update the app.