开发者

how to check each and every object of array in the userdefaults

i am saving selected date from picker in array in userdefaults. So when i select a particular date and click on save that particular date is getting saved in userdefaults.This is done properly.But now i have problem.I need to compare each and every date and time of userdefaults with the current time .If an object gets equivalent to the current date and time then the notification should be shown.How is this possible.Thanks. This is my code Here i am selecting my date through date picker and saving date in userdefaults.

this is my timepickercontroller.m

- (void)viewDidLoad {
     time = [[NSString alloc] init]; 
    CGRect pickerFrame = CGRectMake(0,40,0,0); 

    datePicker = [[UIDatePicker alloc] initWithFrame:pickerFrame]; 
    datePicker.datePickerMode = UIDatePickerModeTime; 
    datePicker.date = [NSDate date];
    [datePicker addTarget:self action:@selector(convertDueDateFormat) forControlEvents:UIControlEventValueChanged]; 
    [self.view addSubview:datePicker]; 

    [datePicker release];


 }

-(void)convertDueDateFormat{

    app = (StopSnoozeAppDelegate*)[[UIApplication sharedApplication]delegate];
    app.timerdate = [self.datePicker date];
    NSLog(@"picker date:%@",selecteddate);  
    if ([[NSUserDefaults standardUserDefaults] valueForKey:@"time"]==nil)
    {
        [[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"time"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        NSLog(@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"time"]);
    }
    else
    {
        NSArray *array = [[NSUserDefaults standardUserDefaults] objectForKey:@"time"];
        NSMutableArray *array_dates = [[NSMutableArray alloc] init];
        for(int i =0;i<[array count];i++)
        {
            [array_dates addObject:[array objectAtIndex:i]];
        }
        [array_dates addObject:app.timerdate];
        [[NSUserDefaults standardUserDefaults] setObject:array_dates forKey:@"time"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        NSLog(@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"time"]);
    }   

   }


this is the controller where i am setting notification


-(IBAction)save{

    dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"hh:mm a"];
    NSDate *currentime = [NSDate date];
    currentcheck = [NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:currentime] ];
    NSLog(@"currenttime:%@",currentcheck);

    timepicker = [[TTimePickerController alloc]init];
        if (app.timerdate == NULL && interval == NULL && newsound == NULL)
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"UIAlertView" message:@"Set Date,interval,sound for alarm" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
        alert = nil;
    }
    else
    {
                    [self scheduleNotification];
        [self saveInDatabase];

    }

}



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


    timepicker = [[TTimePickerController alloc]init];
        NSDate *newtestdate = [[NSUserDefaults standardUserDefaults]objectForKey:@"time"];

    NSLog(@"fireDate=%@", newtestdate);

    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
    NSDateComponents *dateComponents = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:newtestdate];
    NSDateComponents *timeComponents = [calendar components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:newtestdate];


    NSDateComponents *dateComps = [[NSDateComponents alloc]init];
    [dateComps setDay:[dateComponents day]];
    [dateComps setMonth:[dateComponents month]];
    [dateComps setYear:[dateComponents year]];
    [dateComps setHour:[timeComponents hour]];
    [dateComps setMinute:[timeComponents minute]];
    [dateComps setSecond:[timeComponents second]];

    itemDate = [calendar dateFromComponents:dateComps];
    [dateComps release];



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

        UILocalNotification *notif = [[cls alloc]init];
            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:开发者_JAVA技巧notif];
        [notif release];
    }
}


Supposing ur time format is like this hh:mm:ss

NSDate *currentTime=[NSDate date];
NSArray *timeArray=[[NSUserDefaults standardUserDefaults]objectForKey:@"time"];
NSDateFormatter* tempFormat = [[[NSDateFormatter alloc]init]autorelease];
[tempFormat setDateFormat:@"h:mm a"];
NSString* tempStr = [NSString stringWithFormat:@"%@",[tempFormat stringFromDate:date]];
if([tempStr isEqualToString:[NSString stringWithFormat:@"%@",[timeArray objectAtIndex:0]]]){
//Make any decision 
//Similarly compare all the time in the array by changing the index using for-loop.

}


I suspect that what you really want to do is not to compare the dates, but to set the fire date of the notification to the date you got from the array.

To do that, you enumerate the array and launch an NSNotification for each timerDate:

 
    NSArray *timerDates = [NSUser Defaults standardUserDefaults] objectforKey:@"time"];
    NSDate *now = [NSDate date];

    UILocalNotification *localNotif = [[UILocalNotification alloc] init];  
    // .... general setup of all notifs here.

    for (NSDate * aDate in timerDates) {  

       localNotif.fireDate = aDate;  
        //... further setup of each individual notif here....
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];  
    }

    [localNotif release];  

However, from your code, it looks like you're getting a single date from defaults:

NSDate *newtestdate = [[NSUserDefaults standardUserDefaults]objectForKey:@"time"];

but you save an array there in the method

-(void)convertDueDateFormat{
        ...
        [[NSUserDefaults standardUserDefaults] setObject:array_dates forKey:@"time"];

So which one is it? If you have an array, you can test its values as dates:

NSArray *timerDates = [NSUser Defaults standardUserDefaults] objectforKey:@"time"];
NSDate *now = [NSDate date];
_block int timerIndex;  // can be changed inside the block

// use a block to enumerate the array
[timerDates enumerateObjectsUsingBlock:^(id obj,NSUInteger index, BOOL *stop){
    if(fabs([(NSDate *)obj timeIntervalSinceDate:now])< 1.0) {
       *stop=YES;
       timerIndex = index;
    }
} ]; 

Then use timerIndex to get the right date:

localNotif.firedate = [timerDates objectAtIndex:timerIndex];

Definitely also look at the documentation for comparing dates (Apple class documentation for NSDate is good). This is just one way to do it, and probably not the best.
(sorry about my formatting. I'm kinda new at this)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜