开发者

how to compare the array values and check it with the current date and set it ti the notification firedate

开发者 https://www.devze.com 2023-04-11 08:06 出处:网络
i am using an user defaults which contains array of dates .in this array i am storing all the dates selected from the date picker.i now want to compare each of these dates with the current date .when

i am using an user defaults which contains array of dates .in this array i am storing all the dates selected from the date picker.i now want to compare each of these dates with the current date .when the date matches with the current date the notification should be shown .i have done the code for traversing array but it is not working it does not match the date with the current date.And does not display the notification at the proper time. This is my code:

  -(void)scheduleNotification
    {
        [[UIApplication sharedApplication]cancelAllLocalNotifications];


        timepicker = [[TTimePickerController alloc]init];
            //this is my new code.
        NSDate *now = [NSDate date];


        NSMutableArray *array = [[NSUserDefaults standardUserDefaults]objectForKey:@"time"];
        //array is my  array where i am saving all the the userdefaults objects 
        for (NSDate *date in array)
        {
            //here i am comparing my date object with current date object
            if ([date isEqualToDate:now]) 
            {
                itemDate = date;
                NSLog(@"%@",itemDate);
            } 



        }




        Class cls = NSClassFromString(@"UILocalNotification");
        if (cls!= nil) {

            UILocalNotification *notif = [[cls alloc]init];
        //in the notification firedate property i am setting the itemdate.

            notif.fireDate = itemDate;

            [app.dateFormatter setDateFormat:@"hh:mm a"];
            newstring = [app.dateFormatter stringFromDate:notif.fireDate];
            NSLog(@"new fire date:%@",newstring);

            notif.timeZone = [NSTimeZone defaultTimeZone];
            notif.alertBody = @"Alarm";
            notif.alertAction = @"View";
            notif.soundName = UILocalNotificationDefaultSoundName;
            notif.applicationIconBadgeNumber=1;
            notif.repeatInterval = 0;
            [[UIApplication sharedApplication]scheduleLocalNotification:notif];
            [notif release];
        }
    }

//but the problem the date is not getting checked properly and notification 开发者_运维技巧is not displayed at proper time.


You should remove this line from your code and it will work fine. If you call this function, all your old notifications are canceled.

[[UIApplication sharedApplication]cancelAllLocalNotifications];
0

精彩评论

暂无评论...
验证码 换一张
取 消