如何从日期数组创建 ios LocalNotification? [英] How to create ios LocalNotification from array of dates?

查看:25
本文介绍了如何从日期数组创建 ios LocalNotification?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组日期

NSArray *datesArray=[NSArray arrayWithObjects: @"2014-09-14 00:00:00",@"2014-08-21 07:12:36",@"2014-08-14 00:00:00",@"2014-07-14 00:00:00",@"2014-06-14 00:00:00",@"2015-01-01 10:00:00",@"2014-06-14 00:00:00",@"2014-05-14 11:24:15", nil];

现在我想在数组中可用的日期前一天触发如何实施?

now i want to fire one day before the date available in array How to implement it?

我正在尝试这个

    NSDateFormatter *Formatter1 = [[NSDateFormatter alloc] init];
    [Formatter1 setLocale:[[NSLocale alloc] initWithLocaleIdentifier:NSLocaleIdentifier]];
    [Formatter1 setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
    [Formatter1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    NSDate *date1 =[NSDate date];
    NSString *string =[Formatter1 stringFromDate:date1];
    NSLog(@"sring %@",string);
    NSDate *todaydate =[Formatter1 dateFromString:string];
    NSLog(@"2day date is %@",todaydate);

for (int i=0;i<datesArray.count;i++)
{
    NSDate *_date =[Formatter1 dateFromString:[datesArray objectAtIndex:i ]];
    NSLog(@"date is %@",_date);
    if(_date == todaydate){
        localNotif.fireDate = _date;
        localNotif.alertBody = @"festival";
        localNotif.alertAction=@"Show me";
        localNotif.applicationIconBadgeNumber = 1;
        localNotif.soundName = UILocalNotificationDefaultSoundName;

        [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
    }

推荐答案

你可以使用 NSCalendar 函数来减去一天,特别是 dateByAddingComponents

You can subtract a day by using the NSCalendar functions, specifically dateByAddingComponents

NSArray *datesArray = @[@"2014-09-14 00:00:00",@"2014-08-21 07:12:36",@"2014-08-14 00:00:00",@"2014-07-14 00:00:00",@"2014-06-14 00:00:00",@"2015-01-01 10:00:00",@"2014-06-14 00:00:00",@"2014-05-14 11:24:15"];

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setDay:-1];

NSDateFormatter *Formatter1 = [[NSDateFormatter alloc] init];
[Formatter1 setLocale:[[NSLocale alloc] initWithLocaleIdentifier:NSLocaleIdentifier]];
[Formatter1 setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
[Formatter1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

[datesArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    localNotif.fireDate = [calendar dateByAddingComponents:dateComponents toDate:[Formatter1 dateFromString:obj] options:0];
    localNotif.alertBody = @"festival";
    localNotif.alertAction = @"Show me";
    localNotif.applicationIconBadgeNumber = 1;
    localNotif.soundName = UILocalNotificationDefaultSoundName;
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}];

W

这篇关于如何从日期数组创建 ios LocalNotification?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆