2014-09-25 70 views
0

在我的應用程序中,我使用的是UILocalNotifications。我想爲每週的不同日期設置通知。爲此,我在數組中有不同的日期。但是我得到了錯誤的結果。我的代碼有什麼問題嗎?我的代碼是如何設置多個UILocalNotifications

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); 
     } 
+0

您是否檢查過您的specificDate變量每次都獲取不同的日期? – iHulk 2014-09-25 11:01:32

+0

是的,我檢查它是不同的每一次。 – 2014-09-25 11:09:11

+0

你能告訴我們天陣嗎? – karthikeyan 2014-09-25 11:13:24

回答

0

我們已經做了類似於你的問題的東西,但我不知道它會幫助你或不..只是嘗試這個** NSWeekdayCalendarUnit **我建議你只是指我的答案。

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; 
     } 
    } 
} 
+0

這對你有幫助嗎? – karthikeyan 2014-09-26 13:16:41

0

我覺得這裏你濫用線localNotification.repeatInterval = NSWeekdayCalendarUnit;

就會讓一週的每一天是在你的情況下,創設問題的通知重複。請刪除這一行,然後再試一次就可以了。

+0

我也想重複這些通知每週的每一天,因爲我已經解僱了7個日期的通知.ie 7天不同 – 2014-09-25 11:20:57

+0

但是你的每個通知將重複每一天,就像在2014-09-25 11:15:28 +0000發起的通知一樣,然後在2014-09-26會有兩個通知其中一個生成爲25日,另一個生成爲26日。 – iHulk 2014-09-25 11:24:18

+0

同樣在9月30日會有6個通知背對背。 – iHulk 2014-09-25 11:25:47