For example, repeat func getNewSentence()
in the app every 24 hours so that it generates a new sentence/refreshes itself every day at midnight?
This is regardless of whether the user opens the app or not as the sentence should be in the app for a 24 hours before renewing.
Post
Replies
Boosts
Views
Activity
Hello there,
Beginner here - I have a function that gives a "word of the day", but I want it to stay in the device for a full 24 hours (regardless of how many times the app is opened or closed) and then rerun the function again the next day to give a new word.
In order to do this I am trying to user UserDefaults, to save the date and to only run the code when it is a new day. I struggling a lot since all the tutorials online are about using UserDefaults to store user preferences.
This is my terrible code so far which makes no sense...
override func viewDidLoad() {
super.viewDidLoad()
//save date?
UserDefaults.standard.set(Date(), forKey: "lastAccessDate")
let lastAccessDate = UserDefaults.standard.object(forKey: "lastAccessDate") as? Date ?? Date()
//if it is same day show me the content picked from "selectedVocab://
if Calendar.current.isDateInToday(lastAccessDate){
userDefaults.value(forKey: "selectedVocab") =
//if not the same day rerun code
if !Calendar.current.isDateInToday(lastAccessDate) {
updateWordOfTheDay()
}
}
}
//my function
func updateWordOfTheDay() {
let selectedVocab = retrieveVocab() // which returns on set of shuffled data
updateLabels(using: selectedVocab)
sendNotification(using: selectedVocab)
//saves to userdefaults?
userDefaults.setValue(selectedVocab, forKey: "selectedVocab")
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():
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?
I am about to enroll in the Apple Developer Program in order to publish my first app.
The problem is I really do not want to use my real name and have it displayed as the app developer! If you google my name many photos of me come up, my current work place, linkedIn, social media ect and I just do not want to be so obviously linked to that.
I can't enroll as an organisation because I don't have one, and I don't have the money to do so. Is there no way of using some sort of pseudo display name on the app store?
If not this is really unfair and unnecessary on behalf of Apple... I mean I don't mind them knowing my real identity on the back end of things, but I don't want everyone who downloads my app to see my real name...