3

我試圖在每個星期六重複UILocalNotification,但是我沒有成功。 我嘗試以下方法:每週六12:00點的UILocalNotification

// Create notification for every saturday at 12:00 am 
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
NSDateComponents *components = [gregorian components:NSUIntegerMax fromDate:[NSDate date]]; 
[components setWeekday:7]; // Saturday 
[components setHour:12]; // 12 am 
[components setMinute:0]; 
[components setSecond:0]; 

NSDate *fireDate = [gregorian dateFromComponents:components];   
NSLog(@"%@", fireDate); 

[[NotificationsManager sharedManager] configureLocalAlertMessage:message 
                    fireDate:fireDate 
                  repeatIntervalUnit:NSWeekCalendarUnit 
                     type:kNotificationTypeMoneyBox]; 

但 「fireDate」 的回報: 「2013年3月12日11:00:00 +0000」,即今天。

任何幫助? 謝謝!

編輯:

代碼來配置通知

- (void)configureLocalAlertMessage:(NSString *)message fireDate:(NSDate *)date repeatIntervalUnit:(NSCalendarUnit)repeat type:(NotificationType)type { 

    // Local notification 
    UILocalNotification *notification = [[UILocalNotification alloc] init]; 
    notification.timeZone = [NSTimeZone systemTimeZone]; 
    notification.fireDate = date; 
    notification.repeatInterval = repeat; 
    notification.soundName = UILocalNotificationDefaultSoundName; 
    notification.hasAction = YES; 
    notification.alertBody = message; 
    notification.userInfo = @{kNotificationTypeKey : @(type)}; 
    notification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1; 

    // Schedule 
    [[UIApplication sharedApplication] scheduleLocalNotification:notification]; 

} 
+0

其中是您設置本地通知的代碼。此代碼僅用於創建日期。 – John 2013-03-12 15:42:25

+0

我添加了新的信息。謝謝! 「 – mhergon 2013-03-12 15:49:19

+0

」當信息不一致時,日曆可能會忽略某些組件參數,或者該方法可能會返回nil。「 – 2013-03-12 15:54:49

回答

1
NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar]; 
    NSDateComponents *weekdayComponents =[gregorian components:(NSDayCalendarUnit |    NSWeekdayCalendarUnit | NSWeekCalendarUnit | NSYearCalendarUnit) fromDate:idate]; 

    int weekday = [weekdayComponents weekday]; 
    NSDate *nextdate ; 
    NSDateComponents *offsetComponents = [[NSDateComponents alloc] init]; 
    if(weekday <= 5) 
    { 
     /* it is my case to check the weekday, if it is less or greater than 5*/ 
    } 
    else 
    { 
     [offsetComponents setDay:7]; 
     [offsetComponents setHour:12]; 
     [offsetComponents setMinute:0]; 
     [offsetComponents setSecond:0]; 
     nextdate = [gregorian dateByAddingComponents:offsetComponents toDate:idate options:0]; 
    } 
    [offsetComponents release]; 
    [gregorian release]; 

您可以使用此下一個日期作爲消防日期爲本地的通知。 這裏idate是NSDate * idate = [NSDate date];

相關問題