三天后如何触发 uilocalnotification?

How to fire a uilocalnotification after three days?

我目前正在做一个 iPhone 项目。 我想在三天后发出本地通知。

谁能帮我实现这个目标?

提前致谢。

您可以安排任何时间,从当前时间开始,这样,

UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:(60*60*24*3)]; //60 seconds* 60 minutes * 24 hours * 3 days
localNotification.alertBody = [NSString stringWithFormat:@"Set your message",locations];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

更新 1

关于用户通知的权限,你必须在 App Delegate 中设置,

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)])    {
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
    }
    return YES;
}

HTH,享受编码吧!

只是从这里的其他答案更新。

60*60*24 不是一天,也不应该用来表示一天。

如果你想要三天时间的日期那么你可以使用 NSCalendar...

NSDate *now = [NSDate date];

NSCalendar *calendar = [NSCalendar currentCalendar];

NSDateComponents *components = [NSDateComponents new];
components.day = 3;

NSDate *threeDaysTime = [calendar dateByAddingComponents:components toDate:now options:0];

这是三天时间的计算方法。使用 60*60*24 是不正确的。