3

我想uilocalnotification在我的应用程序中实现。但问题是它没有在确切的时间发射。它在给定的发射时间 30 - 40 秒后发射。是否有我遗漏的东西,或者这是UILocalNotification.

谢谢

4

3 回答 3

9

UILocalNotification 是为将来发生的事情而设计的,因此 30 秒的错误就可以了(即,如果我在 15:00 开会,那么在 15:00:30 告诉我不会有问题 :)

如果您想要更高的准确性,则需要保持应用程序运行并使用 NSTimer。

于 2011-02-21T10:28:20.313 回答
3

我知道这是一个老问题,但我刚刚弄清楚了,您可以使用该 dateByAddingTimeInterval :方法。

notif.fireDate = [[datePicker date] dateByAddingTimeInterval:-30];
于 2011-03-28T10:08:15.733 回答
1

您可以fireDate通过 using设置NSDateComponents,您可以在其中设置firedate的秒数。当我将它设置为 0 时不起作用,但如果我将其设置为 1 秒,它就会起作用。与 30-40 秒相比,迟到 1 秒就可以了。

NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *dateComps = [cal components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond   fromDate:fireDate];
    [dateComps setSecond:1];
    [dateComps setNanosecond:00];
    NSDate *newDate = [cal dateFromComponents:dateComps];
    fireDate = newDate;
于 2017-01-12T11:40:25.137 回答