1

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!!!!!!

rocket101
  • 7,369
  • 11
  • 45
  • 64
  • See also: [Wake up an app at a specific time in iOS](http://stackoverflow.com/questions/20708798/wake-up-an-app-at-a-specific-time-in-ios) – Aaron Brager Feb 19 '15 at 15:48

1 Answers1

1

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.

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
  • I don't need to open my app, I just want a push notification to be sent at 7. However, before the push notification is sent, it needs to complete some logic (Fetching and parsing a number to have in the push notification). How do I do this? – rocket101 Feb 19 '15 at 15:27
  • You'll have to do that on the server side. You can't schedule work to be completed at a specific time on the client side. – Aaron Brager Feb 19 '15 at 15:47
  • The link you posted seems to suggest that one can schedule a local "transparent" notification that is not seen, but "wakes up" the app and could let it send a local push notification with the updated data. Is this correct? If so, if you could post an answer describing how, I would appreciate it a lot. Thanks! – rocket101 Feb 19 '15 at 16:06
  • You can create silent push (remote) notifications, but you cannot schedule a silent local notification. – Aaron Brager Feb 19 '15 at 16:32