5

I have been tasked to write an app that allows a user to schedule emails to be sent out in future.

The user selects a date time from a date picker, composes the message and recipient and then schedules the event. When the date/time occurs the message is sent out.

Can someone guide me to how to get about scheduling lets say a text message. I know how to send a text message. Just was not sure on the scheduling aspect of things.

Any pointers will be much appreciated.

Strong Like Bull
  • 11,155
  • 36
  • 98
  • 169
  • Checkout: http://stackoverflow.com/questions/6229759/how-update-a-label-periodically-on-ios-every-second – Gelldur May 17 '16 at 12:38

6 Answers6

8

The first response will technically allow you to establish a timer that will fire every 2.5 seconds, however the original poster asked for a solution that would fire at a specific time. For that you need to use the following method of NSTimer:

- (id)initWithFireDate:(NSDate *)date interval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats

The first argument is an NSDate indicating when the timer should fire.

The original poster did not specify, but if this is an iOS app then it is important to understand that timers scheduled to fire at a distant date/time will not fire if your app is not the foreground app. In fact there is no way to schedule such an event to occur when your app is in the background on iOS, so you must take that into account.

iDev
  • 23,310
  • 7
  • 60
  • 85
Carter
  • 4,738
  • 2
  • 22
  • 24
  • no way to fire an event in future if app is in background? That is a bummer – Strong Like Bull Oct 10 '11 at 18:44
  • 1
    I'm afraid not. If you think about an iOS device and you use it the way it is intended to be, you really can't design something where specific and vital actions need to occur at a predetermined date/time. The phone might be turned off at that point or it might not be nearby or the network is not available. I think Apple put a lot of thought into what applications can do in the background. It may not always be convenient to the developer that things are so tightly controlled in iOS but they are all that way for the benefit of the user. – Carter Oct 10 '11 at 22:37
4

Here's a snippet of code which sets a one use timer to call self's imageSavedLabelOff: selector with itself (the timer) as the object parameter to the method. The timer schedules the call to be made in 2.5 seconds.

NSTimer *quickie = [NSTimer scheduledTimerWithTimeInterval:2.5 target:self selector:@selector(imageSavedLabelOff:) userInfo:nil repeats:NO];

jakev
  • 2,815
  • 3
  • 26
  • 39
1

You may have already found the answer by now but for future visiters like me I would like to suggest an answer- i.e. EventKit :

https://developer.apple.com/library/ios/documentation/DataManagement/Conceptual/EventKitProgGuide/ReadingAndWritingEvents.html

You can schedule/fetch events for any time and do your stuff accordingly. Hope this helps somebody.

atulkhatri
  • 10,896
  • 3
  • 53
  • 89
0

You can use -

[self performSelector:@selector(myFunc:) withObject:nil afterDelay:5.0];
Abhishek Mishra
  • 1,625
  • 16
  • 32
0

You should be able to achieve this using NSRunLoop. Check out the Threading Programming Guide.

lottscarson
  • 578
  • 5
  • 14
0

Apart from the use of NSTimer, you should be aware that sending of the E-Mail can fail for several reasons (no network available and others). Then you need to reschedule the request, maybe give up after 3 retries and notify the user about this.

ott--
  • 5,642
  • 4
  • 24
  • 27