Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从日期数组创建ios LocalNotification?_Ios_Uilocalnotification - Fatal编程技术网

如何从日期数组创建ios LocalNotification?

如何从日期数组创建ios LocalNotification?,ios,uilocalnotification,Ios,Uilocalnotification,我有一系列的日期 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]; 现在

我有一系列的日期

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];
现在我想在数组中可用的日期前一天开火 如何实施

我在试这个

    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];
    }
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%@”,字符串);
NSDate*todaydate=[Formatter1 dateFromString:string];
NSLog(@“2天日期为%@”,今天日期);

对于(int i=0;i您可以使用
NSCalendar
函数减去一天,特别是
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

关于代码样式的小说明-将
Formatter1
更改为
Formatter1
。类应以大写字母开头,变量应以小写字母开头。这将提高代码的清晰度。@Guy Kogus谢谢!!:):谢谢你的回答..每当我运行应用程序本地通知弹出窗口..它也会提前一天弹出?确实如此。查看取消通知,您可以使用
cancelAllLocalNotifications
来执行此操作。如果您有对通知的引用,则可以取消特定通知:
cancelLocalNotification:
。您可以使用
scheduledLocalNotifications
获取参考-这将让您知道所有已安排的通知。在使用此方法添加另一个通知之前,您甚至可以检查是否存在具有相同日期的现有通知。很好,谢谢。还有一个问题..如果上面的datesarray是json对象,并且我想从UILocalNotification的json服务中提取它
[NSJSONSerialization JSONObjectWithData:self.jsonData选项:0错误:NULL]
将返回一个字典,您可以调用它
objectForKey:
on.ohk u mean我解析json,然后它可以动态地与UILocalNotification通信,在指定的日期为该事件提供特定事件的通知。因此,可以在ApplicationIdentinterBackground方法中完成解析,因为我希望在应用程序处于后台状态。