Hello everyone,
I have a function in viewDidLoad() which checks whether the last access date by the user is within today's calendar date. If it is the same day, it proceeds to show the word for that day (retrieved from a JSON file). If it's a new day it displays a new word on the UI.
However, if the app is not manually opened by the user, the content does not update, no new data is stored in userdefaults and therefore the content of my local notification will also not send the user the new word picked for that day.
This is the code that runs in viewDidLoad():
Since this cannot be run unless the app is manually opened, I thought using BGAppRefreshTask would be perfect as it updates the app "with small bits of information". This way the app would be woken up and these conditions would be checked. But I have no idea how to actually implement this, especially in the "launch handler". All the tutorials online deal with data that is retrieved from a server, but my data is all internal.
I just want the app to occasionally run in the background to see if the last access date is different from the current day, and if it is, to send a notification using the new data.
Can someone please advise me?
I have a function in viewDidLoad() which checks whether the last access date by the user is within today's calendar date. If it is the same day, it proceeds to show the word for that day (retrieved from a JSON file). If it's a new day it displays a new word on the UI.
However, if the app is not manually opened by the user, the content does not update, no new data is stored in userdefaults and therefore the content of my local notification will also not send the user the new word picked for that day.
This is the code that runs in viewDidLoad():
Code Block func isRefreshRequired() { // if it is first time opening app if userDefaults.bool(forKey: "First Launch") == false { //run code during first launch _ = updateWordOfTheDay() NotificationManager.askNotificationPermission() print("First time opening app") userDefaults.set(true, forKey: "First Launch") } else { //run code after first launch print("Not first time opening app") userDefaults.set(true, forKey: "First Launch") let lastAccessDate = userDefaults.object(forKey: "lastAccessDate") as? Date ?? Date() userDefaults.set(Date(), forKey: "lastAccessDate") // if it is the same day give the vocab picked for that day if calendar.isDateInToday(lastAccessDate) { readFromUserDefaults() print("No refresh required as it is the same day as lastAccessDate which was \(lastAccessDate)") } else { print("new day since lastAccessDate") _ = updateWordOfTheDay() } } }
Since this cannot be run unless the app is manually opened, I thought using BGAppRefreshTask would be perfect as it updates the app "with small bits of information". This way the app would be woken up and these conditions would be checked. But I have no idea how to actually implement this, especially in the "launch handler". All the tutorials online deal with data that is retrieved from a server, but my data is all internal.
I just want the app to occasionally run in the background to see if the last access date is different from the current day, and if it is, to send a notification using the new data.
Can someone please advise me?