我想在我的应用中实施uilocalnotification
。但问题是它没有在确切的时间开火。它是在给定的射击时间30-40秒后射击。
是否有我遗漏的东西,或者这是UILocalNotification
中常见的想法。
由于
答案 0 :(得分:9)
UILocalNotification是为未来发生的事情而设计的,所以错误30秒就可以了(即如果我在15:00开会,那么在15:00:30告诉我不会有问题:)
如果您需要更高的准确度,则需要保持应用运行并使用NSTimer。
答案 1 :(得分:3)
我知道这是一个老问题,但我只是想出来,你可以使用
dateByAddingTimeInterval :
方法。
notif.fireDate = [[datePicker date] dateByAddingTimeInterval:-30];
答案 2 :(得分:1)
您可以使用fireDate
设置NSDateComponents
,您可以在其中设置触发的秒数。当我将其设置为0时不起作用,但如果我将其设置为1秒,则它可以工作。迟到1秒就可以了30-40秒。
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;