iOS - Program method to execute specific code according to time on the phones clock

Is it possible to execute code when a specific time is reached on an iPhone's clock. Would this be able to work after the iPhone is closed (the screen goes black) but the app was left running (the user did not return to the home screen).

For example, would it be possible for a method to be programmed to go off at 1:30:15 PM with no relation to the current time, and are there any restrictions depending on whether the phone is closed or if the app is running in the background?

I found a similar post here How to generate event on a specific time of clock in C#? but this generates a timer based on the current time to run a method later, instead of using the clock without relation to the current time.

I have created a poor way of doing this-

{

time_t rawtime;
struct tm * timeinfo;

time ( &rawtime );
timeinfo = localtime ( &rawtime );
NSLog(@"TESTSTART - TEST START");
// Check
if(timeinfo->tm_hour == 12 && timeinfo->tm_min == 01 && timeinfo->tm_sec == 48)
{
    NSLog(@"Run Method");

    printf("the time is 12:01:48");
}    else {
    NSLog(@"ELSE");

    [self syncTest];
}
}

This would run a method at 12:1:48, but it is most likely not an acceptable way of doing this. Does anyone know of any better ways to do this and how much strain this way puts on the cpu? Thanks

Answers


Use an NSTimer and set the fireDate for whichever date you want the timer to fire on and then catch the notification that it has fired and perform you task.


Need Your Help

how to make day value clickable in month view

fullcalendar

Is there a way to make the day of the month value clickable in the month view like in Google Calendar?

SQL Server 2008 IN ALL Query

sql sql-server sql-server-2008

Is there a easy way to do an IN ALL command in SQL?