Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

339
Vistas
How to run code at a specific time in swift

I've looked all over but I can't find an answer to this question. I want a specific function to run at 6:00 am and 6:00 pm every day for a day/night mode in my app. This doesn't need to run in the background, just when the app is open, but I also want to prefom other functions of the app as well while it checks for the time.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can do it like this. Just set a Date with the Calendar object. Put your specific time in military hours (from 0 to 24, with 0 being midnight) and Voila!, if your app is in the foreground and the specified time kicks in, you will see the function you used as the selector execute

Give it a shot and happy coding

override func viewDidLoad() {
    super.viewDidLoad()

    let calendar = Calendar.current 

    let now = Date()
    let date = calendar.date(
        bySettingHour: 23,
        minute: 52,
        second: 0,
        of: now)!

    let timer = Timer(fireAt: date, interval: 0, target: self, selector: #selector(runCode), userInfo: nil, repeats: false)

    RunLoop.main.add(timer, forMode: RunLoop.Mode.common)
}

func runCode() {
    print("Do whatever here")
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda