2013-11-21 60 views
9

我想在每個星期日的晚上8點觸發UILocalNotification,但是,我每天晚上8點都會收到火災日期。每週在特定的日子和時間發出通知

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
    NSDate *now = [NSDate date]; 
    NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: now]; 
    [componentsForFireDate setWeekday: 1] ; //for fixing Sunday 
    [componentsForFireDate setHour: 20] ; //for fixing 8PM hour 
    [componentsForFireDate setMinute:0] ; 
    [componentsForFireDate setSecond:0] ; 

    NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForFireDate]; 
    UILocalNotification *notification = [[UILocalNotification alloc] init] ; 
    notification.fireDate = fireDateOfNotification ; 
    notification.timeZone = [NSTimeZone localTimeZone] ; 
    notification.alertBody = [NSString stringWithFormat: @"New updates!"] ; 
    notification.userInfo= [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"New updates added for that week!"] forKey:@"new"]; 
    notification.repeatInterval= NSDayCalendarUnit ; 
    notification.soundName=UILocalNotificationDefaultSoundName; 

    NSLog(@"notification: %@",notification);//it indicates that the notif will be triggered today at 8PM and not Sunday. 

    [[UIApplication sharedApplication] scheduleLocalNotification:notification] ; 

謝謝。

+0

試試這個notification.repeatInterval = NSWeekCalendarUnit; – chancyWu

+0

@Chany:它跳到下週(下週二),但不是在星期天。下面是我用'NSWeekCalendarUnit'所擁有的:'next fire date = 2013年11月26日星期二下午8:00:00' – androniennn

回答

12

您錯過了NSDateComponents init函數中的NSWeekCalendarUnit。添加NSWeekCalendarUnit它並設置repeatIntervalNSWeekCalendarUnit,則輸出爲

next fire date = Sunday, November 24, 2013 at 8:00:00 PM 

的代碼是在這裏:

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
NSDate *now = [NSDate date]; 
NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: now]; 
[componentsForFireDate setWeekday: 1] ; //for fixing Sunday 
[componentsForFireDate setHour: 20] ; //for fixing 8PM hour 
[componentsForFireDate setMinute:0] ; 
[componentsForFireDate setSecond:0] ; 

    //... 
    notification.repeatInterval = NSWeekCalendarUnit; 
    //... 
+0

非常感謝。沒有你,我永遠不會考慮它。謝謝! – androniennn

+0

嗨@chancyWu,我需要在一週內再增加一天。因爲前者需要設置每週星期一和星期二獲得本地通知。我該怎麼辦?在此先感謝 – TamilKing

4

我也搜索一下吧。下面的代碼適合我。將週日值1至7週日至週六以及通知主體與您想要觸發的操作並指定日期,然後通知將在該特定日期發佈。 設定日期時會自動設定。

希望這對你有所幫助。

-(void) weekEndNotificationOnWeekday: (int)weekday :(UILocalNotification *)notification : (NSDate*) alramDate 
{ 
    NSCalendar *calendar = [NSCalendar currentCalendar]; 

    NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: alramDate]; 
    [componentsForFireDate setWeekday: weekday] ; //for fixing Sunday 
// [componentsForFireDate setHour: 20] ; //for fixing 8PM hour 
// [componentsForFireDate setMinute:0] ; 
// [componentsForFireDate setSecond:0] ; 
    notification.repeatInterval = NSWeekCalendarUnit; 
    notification.fireDate=[calendar dateFromComponents:componentsForFireDate]; 
    [[UIApplication sharedApplication] scheduleLocalNotification:notification]; 
} 
+0

如果我給[componentsForFireDate setDate:-1];減去一個? – kiran