![](/img/trans.png)
[英]Schedule UILocalNotificatiions on Every Day or on specific Days of the week?
[英]How to schedule LocalNotifications to fire on Every 2 Days or Every 3 Days.
我有一個必須安排本地通知的應用程序。 考慮到用戶輸入的時間是早上7點,則必須根據他設置的每天(或每2天或每3天)早上7點觸發通知。 必須定期觸發此通知,直到用戶打開通知為止。
我的疑問是如何安排將通知每隔2天,每3天或每2周觸發一次? 抱歉,這是一個非常基本的問題。 我是本地通知的新手。
您需要做一個從開始日期到結束日期的日期列表,然后需要輸入警報天數間隔,並根據該間隔設置警報。
警報需要帶日期的時間。
看到這個功能,這將返回一個日期間隔天的數組
-(NSMutableArray *)getDatesArrayBetweenDates:(NSString *)minDate toDate:(NSString *)maxDate freqDays:(NSInteger )dayfreq
{
// minDate and maxDate represent your date range
NSMutableArray *resultArray=[NSMutableArray array];
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDateComponents *days = [[NSDateComponents alloc] init];
NSDate *minDateObject;
NSDate *maxDateObject;
NSDateFormatter *df=[[NSDateFormatter alloc] init];
[df setDateFormat:@"MM/dd/yyyy"];
minDateObject=[df dateFromString:minDate];
maxDateObject=[df dateFromString:maxDate];
NSInteger dayCount = 0;
while ( TRUE ) {
NSDate *date = [gregorianCalendar dateByAddingComponents: days toDate: minDateObject options: 0];
[days setDay: ++dayCount];
if ( [date compare: maxDateObject] == NSOrderedDescending )
break;
// Do something with date like add it to an array, etc.
NSString *dateForAdd=[df stringFromDate:date];
//if(![self.dateListArray containsObject:dateForAdd])
[resultArray addObject:dateForAdd];
}
[df release];
[days release];
[gregorianCalendar release];
NSMutableArray *returnableArray=[NSMutableArray array];
NSInteger day=0;
for(int i=0;i<[resultArray count];i++)
{
if(day==0)
{
[returnableArray addObject:[resultArray objectAtIndex:i]];
}
if(day==dayfreq)
day=0;
else {
++day;
}
}
return returnableArray;
}
您需要以字符串形式傳遞開始日期和結束日期以及警報的天間隔,此函數將以字符串形式返回日期數組。現在您需要使用這些日期並附加時間,然后通過NSDateFormatter
創建日期對象,然后設置用於本地通知的fireDate
。
查看此鏈接以獲取更多信息
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [put a NSDate object here];
localNotification.repeatInterval = NSDayCalendarUnit;
這將創建每天重復的本地通知。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.