UILocalNotification end date

孤人 提交于 2019-12-04 18:41:49

There is not such option in UILocalNotification as you can read in the documentation.

Your only option is to check wether the year is over when every the user starts the app.

In the UILocalNotification object, I'd recommend setting the repeatInterval property and putting the end date in the userInfo dictionary for querying later to determine if the notification has expired. For example:

UILocalNotification* uiLocalNotification;
uiLocalNotification = [[UILocalNotification alloc] init]; 

//Set the repeat interval
uiLocalNotification.repeatInterval = NSCalendarUnitDay;

NSDate* fireDate = ...

//Set the fire date
uiLocalNotification.fireDate = fireDate;

//Set the end date
NSDate* endDate = ...

uiLocalNotification.userInfo = @{
                                  @"endDate": endDate
                                };

UIApplication* application = [UIApplication sharedApplication];
[application scheduleLocalNotification:uiLocalNotification];

//...

//Somewhere else in your codebase, query for the expiration and cancel, if necessary

UIApplication* application = [UIApplication sharedApplication];
NSArray* scheduledNotifications = application.scheduledLocalNotifications;

NSDate* today = [NSDate date];

for (UILocalNotification* notification in scheduledNotifications) {

    NSDate* endDate = notification.userInfo[@"endDate"];

    if ([today earlierDate:endDate] == endDate) {
        //Cancel the notification
        [application cancelLocalNotification:notification];
    }
}
Use following code:

UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.repeatInterval = NSDayCalendarUnit;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!