iosswiftbackground-process

Swift Background Execution at certain time each morning


I am trying to run a particular function with background execution at a certain time each morning. Here's what I have figured out...

func getCurrentTime() { //Is called every minuite by a timer
    let date = NSDate()
    let formatter = NSDateFormatter()
    formatter.timeStyle = .ShortStyle
    var stringValue = formatter.stringFromDate(date)
    if stringValue == "7:00 AM" {
        sendPushAlert()//Defined elsewhere in code, executes desired code
    }
}

However, that does not run in the background (e.g. when app not open), and seems like a clunky way to do things.

So, How do I run background execution at a certain time each morning?

Or, is there a better way to fetch data to show in a push notification?

Thanks!!!!!!


Solution

  • You can't force iOS to launch your app at a specific time.

    If all you want to do is send a notification at a specific time, schedule a local notification. If the user opens your notification then your app will launch.