gpt4 book ai didi

ios - 找出最后触发的 UILocalNotification

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:58:08 25 4
gpt4 key购买 nike

我正在开发一个使用 UILocalNotifications 的 iOS 闹钟应用。到目前为止一直很好,直到有人想出显示从用户所在位置到他想去的位置的路线的想法。

为此,我想我也许可以获得最后一次触发的 UILocalNotification(所有这些都将 repeatInterval 设置为每周或每天)。

这就是整个麻烦的来源,因为 fireDate 是我第一次安排它们:这是我的场景:

        UILocalNotification* ln = [[UILocalNotification alloc] init];
ln.fireDate = date; // For instance 07/19/2014 10:00:00 (Saturday)
ln.repeatInterval = NSDayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:ln];

UILocalNotification* ln = [[UILocalNotification alloc] init];
ln.fireDate = date; // For instance 07/17/2014 11:00:00 (Thursday)
ln.repeatInterval = NSWeekCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:ln];

想象一下:

下周四 (07/24/2014 11:00:01) 我想知道最后一个触发的 UILocalNotification 是第二个只在周四重复的,第二天我想要第一个,因为它每天重复。

我尝试按日期对所有 LocalNotifications 进行排序,但没有成功。

有没有人遇到过类似的情况?

问候

最佳答案

即使通过 repeatInterval 属性安排它自己重复,iOS 也会在触发后丢弃本地通知。

[UIApplication sharedApplication].scheduledLocalNotifications 实际上会返回计划在 future 触发的所有通知。 所以他们的fireDate将被设置为 future 的一个日期。

经过进一步调查,fireDate 似乎始终设置为创建通知时指定的原始值。然而,下一个开火日期,一个计算值,将在未来。要查看此值,只需在调试器中运行 po yourNotificationObj,您将看到以下类型的输出,其中包括下一个触发日期:

{fire date = Saturday, July 26, 2014 at 1:42:50 PM Eastern Daylight Time, time zone = (null), repeat interval = NSMinuteCalendarUnit, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Saturday, July 26, 2014 at 1:43:50 PM Eastern Daylight Time, user info = (null)}

您如何计算下一个点火日期?请参阅以下摘自 How to grab the NEXT fire date from a UILocalNotification object 的代码:

NSCalendar *calendar = localNotif.repeatCalendar;
if (!calendar) {
calendar = [NSCalendar currentCalendar];
}

NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
// If you use some other repeat interval you would have to change the code accordingly.
// If you were to use NSMonthCalendarUnit you would have
// to use `components.month = 1' instead
components.day = 1;
NSDate *nextFireDate = [calendar dateByAddingComponents:components toDate:localnotif.fireDate options:0];

如果您想记住最后触发的本地通知,则必须将该逻辑构建到您的应用中并保存所需信息。例如,您可以执行以下操作:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

if (localNotification) {
[self saveLocalNotification:localNotification];
}

return YES;
}

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
[self saveLocalNotification:notification];
}

- (void)saveLocalNotification:(UILocalNotification *)notification
{
// save information about the last fired local notification on disk
}

- (UILocalNotification *)lastFiredLocalNotification
{
// read the information stored on disk by the saveLocalNotification: method
// and recreate the notification. I'm just saying recreate the notification
// because I don't know what exactly you're trying to do. You
// can return anything you want here
}

使用上述设置,您可以在需要时调用lastFiredLocalNotification 方法。

关于ios - 找出最后触发的 UILocalNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24844731/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com