【问题标题】:Get time only of next iOS local notification仅获取下一个 iOS 本地通知的时间
【发布时间】:2013-07-14 17:12:44
【问题描述】:

为我的应用获取下一个本地通知集的时间的最佳方法是什么?

我知道可以使用以下循环来获取通知,但这是否总是按时间顺序排序,以便我可以获取项目 [0] 的时间,还是按照添加时间的顺序?怎么能从中拉出时间呢?我需要获取整个日期并格式化超时,还是有更好的方法?

UIApplication *app = [UIApplication sharedApplication];
NSArray *eventArray = [app scheduledLocalNotifications];
for (int i=0; i<[eventArray count]; i++)
{
    UILocalNotification* oneEvent = [eventArray objectAtIndex:i];
    //oneEvent is a local notification
    //get time of first one
}

非常感谢!

山姆

【问题讨论】:

    标签: ios objective-c uilocalnotification


    【解决方案1】:

    这确实是两个问题。首先,如何获得下一个通知。其次,如何仅获取该通知日期的时间部分。

    第一,sorting an array by a date property of the contained objects

    NSSortDescriptor * fireDateDesc = [NSSortDescriptor sortDescriptorWithKey:@"fireDate" ascending:YES];
    NSArray * notifications = [[UIApplication sharedApplication] scheduledLocalNotifications] sortedArrayUsingDescriptors:@[fireDateDesc]]
    UILocalNotification * nextNote =  [notifications objectAtIndex:0];
    

    两个,get just the hours and minutes from the date

    NSDateComponents * comps = [[NSCalendar currentCalendar] components:(NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit) 
                                                               fromDate:[notification fireDate]];
    // Now you have [comps hour]; [comps minute]; [comps second];
    
    // Or if you just need a string, use NSDateFormatter:
    NSDateFormatter * formatter = [NSDateFormatter new];
    [formatter setDateFormat:@"HH:mm:ss"];
    NSString * timeForDisplay = [formatter stringFromDate:[notification fireDate]];
    

    【讨论】:

      【解决方案2】:

      您无法保证scheduledLocalNotifications 数组的顺序。如果您需要在应用程序中多次获取最新通知,我建议使用包含for 循环的方法在UIApplication 上创建一个实用程序类别。这样你就可以打电话了:

      notif = [[UIApplication sharedApplication] nextLocalNotification];
      

      不要重复自己。

      【讨论】:

      • 我只需要一次。该代码是我取得了多远的样本。我不知道如何具体找到下一个的时间。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-05
      • 1970-01-01
      相关资源
      最近更新 更多