【发布时间】:2015-07-14 11:05:11
【问题描述】:
我在过去 2 天遇到了一个问题。我的要求是这样的。
我有一个开始日期作为字符串。喜欢:start_Date == 15-07-15
结束日期为字符串。喜欢:end_Date == 16-07-15
还有时间:开始时间 == 16:28,结束时间 == 18:28
我将 2 个字符串合并并转换为NSDate。
NSString *strt_Date = [NSString stringWithFormat:@"%@ %@",start_Date, start_Time];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
[dateFormat setDateFormat:@"dd-MM-yy HH:mm"];
NSDate *date = [dateFormat dateFromString:strt_Date];
输出是这样的:
date == 2015-07-15 11:01:00 +0000
然后我触发通知:
[self scheduleNotificationForDate:date];
- (void)scheduleNotificationForDate:(NSDate *)date
{
// Here we cancel all previously scheduled notifications
[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSLog(@"date == %@",date);
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = date;// this sets when notification will be called.
// Notification details
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = @"Time to read your course";// this shows the alert box with message.
// Set the action button
localNotif.alertAction = @"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = -1;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
注意:我的设备也是 24 小时格式,而这个日期格式也是 24 小时格式。
我的问题是通知没有触发。请帮帮我。
【问题讨论】:
-
关于这两个主题有很多问题(例如stackoverflow.com/questions/20949897/…),你应该使用NSDateFormatter developer.apple.com/library/mac/documentation/Cocoa/Reference/…
-
您的日期格式是“dd-MM-yy HH:mm”,那么为什么输出为“2015-07-15 11:01:00 +0000”
-
是这样的。 :)
-
如果您在控制台中遇到任何权限错误,您还必须添加以下代码。 if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) { [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeSound|UIUserNotificationTypeBadge categories:nil]]; }
-
我已经做到了。通知未触发..
标签: ios datepicker uilocalnotification