質問

I wonder if it's possible to set end date for UILocalNotification?

I want my notification to fire everyday (NSDayCalendarUnit) but I have end date which I cannot cross (deadline) e.g. I'm taking a photo of my growing mustache everyday for one year period and after a year notifications won't be displayed.

I hope you got my point of view...

役に立ちましたか?

解決

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;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top