获取下一个iOS本地通知的时间

sam*_*les 0 objective-c ios uilocalnotification

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

我知道可以使用以下循环来获取通知,但是这总是按时间顺序排序,所以我可以获得item [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
}
Run Code Online (Sandbox Code Playgroud)

非常感谢!

山姆

Jos*_*ell 6

这真的是两个问题.首先,如何获得下一个通知.其次,如何获得该通知日期的时间组件.

第一,按包含对象的日期属性对数组进行排序

NSSortDescriptor * fireDateDesc = [NSSortDescriptor sortDescriptorWithKey:@"fireDate" ascending:YES];
NSArray * notifications = [[UIApplication sharedApplication] scheduledLocalNotifications] sortedArrayUsingDescriptors:@[fireDateDesc]]
UILocalNotification * nextNote =  [notifications objectAtIndex:0];
Run Code Online (Sandbox Code Playgroud)

二,从日期算起,只需几小时和几分钟

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]];
Run Code Online (Sandbox Code Playgroud)