1

I want to call a method at the same time every day (at the end of the day, like 23.59). I understand to set a repeating method to be called I do like this:

let date = Date()
let timer = Timer(fireAt: date, interval: 86400, target: self, selector: 
#selector(runCode), userInfo: nil, repeats: true)
RunLoop.main.add(timer, forMode: RunLoopMode.commonModes)

This repeats every 86400 seconds, which mean every day. But how to set the date parameter to start at 23.59, the specific time I want. Currently, it just goes off at the time the code calls it.

Thanks very much

RJB
  • 1,704
  • 23
  • 50
  • 1
    There's no sense in creating a timer with an interval of 1 day. It will only work if the user keeps your app running in the foreground for that entire day. – rmaddy May 23 '18 at 17:49
  • Does this method not get called if the app is closed, or in background? – RJB May 23 '18 at 17:50
  • No, the user isn't using your app, the timer won't run. – rmaddy May 23 '18 at 18:20

1 Answers1

1

sounds like you might have to do silent notifications:

Silent Push Notification Payload

What is difference between remote notification and silent notification in iOS?

There is also a UNNotifcationRequest you can look into but it's not silent:

Use UNNotificationRequest (UserNotificationFramework) for data only local notification on iOS

Aaron Halvorsen
  • 2,610
  • 1
  • 23
  • 31