Ios 如何设置多个UILocalNotification
在我的应用程序中,我使用UILocalNotifications。我想为一周中的不同日子设置通知。为此,我在一个数组中有不同的日期。但是我得到了错误的结果。我的代码有什么问题吗。我的代码是Ios 如何设置多个UILocalNotification,ios,objective-c,iphone,uilocalnotification,Ios,Objective C,Iphone,Uilocalnotification,在我的应用程序中,我使用UILocalNotifications。我想为一周中的不同日子设置通知。为此,我在一个数组中有不同的日期。但是我得到了错误的结果。我的代码有什么问题吗。我的代码是 for(int counter=0 ;counter<[daysArray count]; counter++) { int day = [[daysArray objectAtIndex:counter] intValue]; NSDat
for(int counter=0 ;counter<[daysArray count]; counter++)
{
int day = [[daysArray objectAtIndex:counter] intValue];
NSDate *specificDate = [self getDateOfSpecificDay:day];
UILocalNotification *localNotification = [[UILocalNotification alloc]init];
localNotification.fireDate = specificDate;
localNotification.repeatInterval = NSWeekdayCalendarUnit;
localNotification.soundName = sound;
localNotification.alertBody = [NSString stringWithFormat:@"%@",specificDate];
localNotification.alertAction = @"Show me the item";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
NSLog(@"%@",localNotification);
}
我认为您在这里误用了localNotification.repeatInterval=NSWeekdayCalendarUnit
它会让您的通知在一周中的每一天重复,这会在您的案例中造成问题。请删除这一行,然后再试一次。它会起作用。我们已经做了一些与您的问题类似的事情,但我不知道它是否会对您有所帮助。试试这个**n工作日日历单元**我建议您参考我的答案 NSCalendar*gregCalendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar]
NSDateComponents *dateComponent = [gregCalendar components:NSYearCalendarUnit|NSWeekCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];
NSArray *dobArray = [NSArray arrayWithObjects:@"02-08-2014", @"10-08-2014", @"14-08-2014", @"15-08-2014", @"16-08-2014", @"17-08-2014", @"18-08-2014", @"22-08-2014", @"28-08-2014", @"29-08-2014",nil];
NSArray *messagesArray = [NSArray arrayWithObjects:@"Literacy And Numeracy workshop for parents and children", @"raksha Bandhan", @"Tri Colour Dat(School celebration)", @"Independence day (School Holiday)", @"PTM/Book Exhibition", @"Janmastami", @"Parsi New Year- School Holiday", @"Clown Day", @"Eco Friendly Ganesha-School Holiday", @"Ganesh Chaturthi-School Holiday",nil];
for (NSString *dobStr in dobArray)
{
NSArray *components = [dobStr componentsSeparatedByString:@"-"];
if(components.count>2) {
NSInteger aDay = [[components objectAtIndex:0] integerValue];
NSInteger aMonth = [[components objectAtIndex:1] integerValue];
// NSInteger aYear = [[components objectAtIndex:2] integerValue];
if(aDay == [dateComponent day] && aMonth == [dateComponent month]) { // dob is here
[dateComponent setDay:aDay];
[dateComponent setMonth:aMonth];
[dateComponent setYear:[dateComponent year]];
[dateComponent setHour:16];
[dateComponent setMinute:54];
UIDatePicker *dp = [[UIDatePicker alloc] init];
[dp setDate:[gregCalendar dateFromComponents: dateComponent]];
UILocalNotification *notification = [[UILocalNotification alloc] init];
NSInteger index = [dobArray indexOfObject:dobStr];
// [notification setFireDate:[NSDate dateWithTimeIntervalSinceNow:10]];
[notification setAlertBody:[messagesArray objectAtIndex:index]];
[notification setFireDate:dp.date];
[notification setTimeZone:[NSTimeZone defaultTimeZone]];
[application setScheduledLocalNotifications:[NSArray arrayWithObject:notification]];
return YES;
}
}
}
您是否检查了specificDate变量每次获取的日期是否不同?是的,我检查了它每次获取的日期是否不同。能否向我们显示days数组?请提供您获取的数据NSLog@specificDate==%@,具体日期;2014-09-28 11:15:28+0000 2014-09-29 11:16:01+0000 2014-09-30 11:16:13+0000 2014-10-01 11:16:25+0000 2014-09-25 11:16:38+0000 2014-09-26 11:16:57+0000 2014-09-27 11:17:07+0000我还想在一周中的每一天重复这些通知,因为我已经发布了7个日期的通知天但您的每个通知将每天重复,就像2014-09-25 11:15:28+0000发布的通知一样,2014-09-26将有两个通知,一个是25日生成的,另一个是26日生成的。同样地,9月30日将有6个通知背靠背。那么,如果我想每周在同一时间和同一天重复,我必须使用什么?好的,我的错误,我可以吗知道你得到了什么样的错误结果吗?