本地通知问题

时间:2013-03-18 11:09:38

标签: iphone ios localnotification

我正在使用以下代码进行本地通知

 for (int i=0;i<newBirthDates.count;i++)
{
    NSDate *date =[Formatter1 dateFromString:[newBirthDates objectAtIndex:i ]];
    NSComparisonResult result = [date compare:todaydate];
    if(result == NSOrderedSame)
    {
        UILocalNotification *localNotif = [[UILocalNotification alloc] init];
        localNotif.fireDate = date;
        localNotif.timeZone = [NSTimeZone defaultTimeZone];
        localNotif.alertBody = @"birthdate notification";
        localNotif.alertAction = @"View";

        localNotif.soundName = UILocalNotificationDefaultSoundName;
        localNotif.applicationIconBadgeNumber = 1;
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
    }
}
  newBirthdate is array with dates in it
  Formatter1 =MM/dd.

让我们说newbrithdate中的一个日期是18/3,所以我所做的就是更换我的电脑 日期到17/3和11.59 pm然后我等待1分钟它转18/3但我没有得到任何通知  我的模拟器中的时间总是与我的电脑时间相同。

1 个答案:

答案 0 :(得分:0)

您将格式化的日期字符串存储在数组中(以MM / dd格式)并将其转换回日期以设置通知。

让我们做一个简单的检查!!

NSDateFormatter *Formatter1 = [[NSDateFormatter alloc]init];
Formatter1.dateFormat =@"MM/dd";

NSDate *today = [NSDate date];// take current date
NSString *initialDateString =[Formatter1 stringFromDate:today]; // formatted date string(in MM/dd)


NSDate *dateFromFormatter = [Formatter1 dateFromString:initialDateString]; // Date from the formatted string(In your case taking from array)
NSString *finalDateString = [Formatter1 stringFromDate:dateFromFormatter];// formatted string from new date

// lets take a look wether both are same
NSLog(@"Initial date string: %@",initialDateString);
NSLog(@"Final date string  : %@",finalDateString);
// Both date strings are same

NSLog(@"Actual date    : %@",today);
NSLog(@"Formatted date : %@",dateFromFormatter);
//But see both dates are entirely different

输出

初始日期字符串:03/18
 截止日期字符串:03/18
 实际日期:2013-03-18 12:15:45 +0000
 格式化日期:2000-03-17 18:30:00 +0000

那你怎么得到通知?