简体   繁体   中英

How do I cancel an iOS local notification after a particular time?

I want to cancel an iOS local notification after a particular time.

For example : a week later

- (void)ViewDidLoad
{
    NSDate *date = [NSdate date];
    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    //set some localNotif's properties
    localNotif.repeatInterval = NSDayCalendarUnit;
    localNotif.fireDate = date

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}

How can I cancel localNotif after a week (7 days) and can you show me the code?

You can't do that unless your app is running in the foreground at the time the notification is due to be cancelled (in which case there would be no need to cancel it anyway).

The reason you can't do it is because you would need a timer to tell you when to cancel it, and you can't schedule a timer unless you are an app that has a background mode, in which case you could schedule a timer to notify you - but even background apps can be suspended still it would not be guaranteed.

See here iOS Run Code Once a Day

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM