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)
非常感谢!
山姆
这真的是两个问题.首先,如何获得下一个通知.其次,如何获得该通知日期的时间组件.
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)
| 归档时间: |
|
| 查看次数: |
769 次 |
| 最近记录: |