关于 iphone:xcode – 定时本地通知(每天早上 6 点) | 珊瑚贝

xcode – scheduled local notification with time ( Everyday at 6AM )


我使用的是 Xcode 4.3.2,我如何在每天早上 6 点设置此本地通知”dateToFire”?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
(void)notification
{
    UILocalNotification *localNotification = [[[UILocalNotification alloc] init] autorelease];

    if (!localNotification)
        return;

    // Current date
    NSDate *date = [NSDate date];
    NSDate *dateToFire = //Everyday: 6AM;

    // Set the fire date/time
    [localNotification setFireDate:dateToFire];
    [localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
    [localNotification setAlertBody:@“Notification” ];      

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

(void)viewDidLoad
{
    [super viewDidLoad];
    [self notification];
}


使用 CalendarComponents 将小时设置为 6,并将 localNotification.repeatInterval 设置为 NSDayCalendarUnit

1
2
3
4
5
6
NSCalendar *calendar = [NSCalendar currentCalendar]; // gets default calendar
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]]; // gets the year, month, day,hour and minutesfor today’s date
[components setHour:18];
[components setMinute:0];

localNotification.fireDate = [calendar dateFromComponents:components];

  • 感谢您的快速回复,但它有一些错误。 1. 未找到类方法\\’calendar\\'(返回类型默认为\\’id\\’) 2. 使用未声明的标识符\\’NSMinuteCalendarComponents\\’。
  • NSMinuteCalendarComponents 是 NSMinuteCalendarUnit,对不起)
  • 并且日历应该是 currentCalendar)
  • 我已将 \\’NSCalendarComponents\\’ 更改为 \\’NSDateComponents\\’,现在它工作正常。谢谢你。


来源:https://www.codenong.com/10266336/

微信公众号
手机浏览(小程序)
0
分享到:
没有账号? 忘记密码?