首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >过去约会的NSNotifications

过去约会的NSNotifications
EN

Stack Overflow用户
提问于 2016-01-17 21:44:51
回答 1查看 109关注 0票数 0

我正在编写一个应用程序,有很多日期,是基于过去。比如结婚纪念日。假设这个日期是2000年12月25日。

用户从日期选择器中选择此日期,然后将日期保存到用户的设备中。(想象一下保存的日期是2000年12月25日)

在考虑如何编写NSNotifications代码时,我意识到我最大的任务(现在看来是不可能的)是如何向用户发送一个未来的日期,但基于过去的日期。

示例

周年纪念日是二000年十二月二十五日。

每年12月25日提醒用户。

我想一定有办法,但我的搜索却空手而归。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-17 22:19:43

不确定您使用的是哪种语言,但是这里的基本逻辑是,一旦用户选择了日期,为关闭日期设置一个本地通知,然后将重复设置为kCFCalendarUnitYear

目标C中的示例代码

代码语言:javascript
复制
-(void)setAlert:(NSDate *)date{
    //Note date here is the closest anniversary date in future you need to determine first
    UILocalNotification *localNotif = [[UILocalNotification alloc]init];
    localNotif.fireDate = date;
    localNotif.alertBody = @"Some text here...";
    localNotif.timeZone = [NSTimeZone defaultTimeZone]; 
    localNotif.repeatInterval = kCFCalendarUnitYear; //repeat yearly
    //other customization for the notification, for example attach some info using 
    //localNotif.userInfo = @{@"id":@"some Identifier to look for more detail, etc."};
   [[UIApplication sharedApplication]scheduleLocalNotification:localNotif];
}

一旦设置了警报并触发了警报,就可以通过实现AppDelegate.m文件中的通知来处理

代码语言:javascript
复制
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler{
    //handling notification code here.
}

编辑:

对于如何获得最近的日期,您可以实现一个方法来完成这个任务。

代码语言:javascript
复制
-(NSDate *) closestNextAnniversary:(NSDate *)selectedDate {
    // selectedDate is the old date you just selected, the idea is extract the month and day component of that date, append it to the current year, if that date is after today, then that's the date you want, otherwise, add the year component by 1 to get the date in next year
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSInteger month = [calendar component:NSCalendarUnitMonth fromDate:selectedDate];
    NSInteger day = [calendar component:NSCalendarUnitDay fromDate:selectedDate];
    NSInteger year = [calendar component:NSCalendarUnitYear fromDate:[NSDate date]];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setYear:year];
    [components setMonth:month];
    [components setDay:day];
    NSDate *targetDate = [calendar dateFromComponents:components];
    // now if the target date is after today, then return it, else add one year 
    // special case for Feb 29th, see comments below 
    // your code to handle Feb 29th case.
    if ([targetDate timeIntervalSinceDate:[NSDate date]]>0) return targetDate;
    [components setYear:++year];
    return [calendar dateFromComponents:components];
}

你需要思考的一件事是如何对待二月二十九号,你是想在二月二十八号(非闰年)每年报警,还是每四年一次呢?然后,您需要实现自己的逻辑。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34844098

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档