2014-10-30 29 views
-1

我正在尋找使用UILocalNotification向我的應用程序添加提醒功能,但是有64個註冊通知的限制,並且我的應用程序計算出用戶可能會超出該限制。如何獲得與週三相匹配的未來3個月的NSD列表?

因此,我現在正在考慮記錄用戶提醒設置並創建存儲在覈心數據中的未來通知隊列。

每次啓動應用程序時它會檢查有多少個通知與UILocalNotification被註冊,如果數字是< 40將分配從核心數據在未來24只計劃通知到最糟糕的是回升到64

我目前正在努力研究如何計算現在和未來3個月之間可能發生的確切日期,並且我只對選定日期即星期三的日期感興趣。

在此先感謝。

亞倫

+3

那麼,你有一個問題,或者你要我們來看看爲NSCalendar和NSDateComponents的文件,並告訴你它說什麼? – 2014-10-30 15:25:03

+1

使用'NSCalendar'方法'dateByAddingComponents:toDate:options:'獲取下一個'NSDate',並重復,直到你有所需數量的新日期。 – Rob 2014-10-30 15:43:09

回答

1

多虧了別人,我需要閱讀繼承人我結束了與一些幫助做文檔的任意球。

// Current Date + Time given for initial reminder 
NSString *[email protected]"28-10-2014 2:15:00 PM"; 

int dayOfWeekToRemind = 4; // Wednesday 

NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
[formatter setTimeZone:[NSTimeZone systemTimeZone]]; 
[formatter setDateFormat:@"dd-MM-yyyy h:mm:ss a"]; 


// Convert to NSDate 
NSDate *alarmDate = [formatter dateFromString:sDateGiven]; 

NSCalendar *calendar = [NSCalendar currentCalendar]; 



// Now we need to work out Next Wednesday 
NSDateComponents *componentsForFireDate1 = [calendar components:(NSYearCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit | NSWeekOfYearCalendarUnit) fromDate: alarmDate]; 

[componentsForFireDate1 setWeekday: dayOfWeekToRemind] ; // Set to Wednesday 

// Check if the date given is before or after wednesday as we are only interested in future Wednesdays 

if ([componentsForFireDate1 weekday] > dayOfWeekToRemind) { 
    // If greater we add a week 
    [componentsForFireDate1 setWeekOfYear: [componentsForFireDate1 weekOfYear] + 1]; 
} 

// Now we have the very first start date for the next Wednesday 
alarmDate = [calendar dateFromComponents:componentsForFireDate1]; 

int i = 0; 

// Loop through 30 weeks 
for (i = 0; i < 30; i++) { 

    NSDateComponents *componentsForFireDate = [[NSDateComponents alloc] init]; 

    [componentsForFireDate setWeekday: 7] ; // Add 7 days 

    alarmDate = [calendar dateByAddingComponents:componentsForFireDate toDate:alarmDate options:0]; 
    NSLog(@"Date = %@", alarmDate); 
} 

然後再輸出如下:

Given Date Date 2014-10-28 14:15:00 +0000 
ADJUST DATED 2014-10-29 14:15:00 +0000 
Date = 2014-11-05 14:15:00 +0000 
Date = 2014-11-12 14:15:00 +0000 
Date = 2014-11-19 14:15:00 +0000 
Date = 2014-11-26 14:15:00 +0000 
Date = 2014-12-03 14:15:00 +0000 
Date = 2014-12-10 14:15:00 +0000 
Date = 2014-12-17 14:15:00 +0000 
Date = 2014-12-24 14:15:00 +0000 
Date = 2014-12-31 14:15:00 +0000 
相關問題