0

我想在午夜执行我的方法,但我不知道如何正确执行此操作。每分钟检查一次时间,如果当前时间等于 23:59 运行方法看起来很愚蠢。计算从现在到午夜的时间间隔并将其设置为 nstimer 的参数?

你能帮我解决这个问题吗?

更新:对不起,伙计们,我忘了一件事:方法必须在没有任何用户通知的情况下运行后台。

4

2 回答 2

1

您可以使用本地通知:

UILocalNotification *notif = [[UILocalNotification alloc] init];
notif.soundName = @"sound.wav";
notif.alertBody = @"My Message";
notif.alertAction = @"Open";

// you need to configure interval and firedate to schedule everything
notif.repeatInterval = XXX;
notif.repeatCalendar = XXX;
notif.fireDate       = XXX;

[[UIApplication sharedApplication] presentLocalNotificationNow:notif];

[notif release];

要检测应用程序何时从通知打开,您需要在您的 App Delegate 中实现以下方法:

- (void)applicationDidEnterBackground:(UIApplication *)application
- (void)applicationDidBecomeActive:(UIApplication *)application
- (void)applicationWillTerminate:(UIApplication *)application

希望这可以帮助。


编辑后:

你可以使用[NSTimer ScheduleTimerWith.....]方法。

于 2012-08-10T11:13:03.583 回答
0

使用在某个时间安排的本地通知

于 2012-08-10T10:24:02.663 回答