0

以前,我一直在处理本地通知,但现在我遇到了问题。假设今天,我必须在我的应用程序中设置日期前 1 天的通知:07/07/2012。我怎么能做到这一点。我正在设置这样的通知..

- (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];这条线时,日期不准确,所以我无法正确设置本地通知。如果我做错了什么,请帮帮我。

4

1 回答 1

0

NSDateFormatter 的行为与您想象的不同。如果您对它是否使用 GMT、UTC、基于我们当前系统区域设置的时区或其他内容感到困惑,请阅读此问答:NSDateFormatter return wrong date from string

于 2012-06-17T20:58:49.220 回答