以前,我一直致力于本地通知,但现在我遇到了问题。假设今天,我必须在日期前一天设置通知:2012年7月7日在我的应用程序中。我怎样才能做到这一点。我正在设置这样的通知..
- (IBAction) scheduleNotification:(id) sender {
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSDateFormatter *formatter = [[[NSDateFormatter alloc]init]autorelease];
for (int i = 0; i< [delegate.viewController.contactList count] ; i++) {
UILocalNotification *localNotification = [prototypeNotification copy];
NSString *name = [[delegate.viewController.contactList objectAtIndex:i]objectForKey:NAME_KEY];
NSString *birthday = [[delegate.viewController.contactList objectAtIndex:i]objectForKey:BIRTHDAY_KEY];
if (birthday) {
[formatter setDateFormat:@"MM/dd/yyyy"];
NSDate *date = [formatter dateFromString:birthday];
if (date == nil) {
[formatter setDateFormat:@"MM/dd"];
NSDate *date = [formatter dateFromString:birthday];
}
else {
}
self.alarmTime = [date dateByAddingTimeInterval:10];
localNotification.fireDate = alarmTime;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = 1;
NSString *itemName = @"B'day Alert!!!";
NSString *msgName = [NSString stringWithFormat:@"Celebrate %@'s B'day",name];
NSDictionary *userDict = [NSDictionary dictionaryWithObjectsAndKeys:itemName,MessageKey, msgName,TitleKey, nil];
localNotification.userInfo = userDict;
localNotification.soundName = self.soundName;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
else {
// write else part
}
[localNotification release];
}
}
当我遇到NSDate *date = [formatter dateFromString:birthday];
这一行时,日期没有准确到来,所以我无法正确设置本地通知。如果有任何问题,请帮帮我。
答案 0 :(得分:0)
NSDateFormatter的行为与您想象的行为不同。如果您对使用GMT,UTC,基于当前系统区域设置的时区或其他内容感到困惑,请阅读此问题并回答:NSDateFormatter return incorrect date from string