iphone - xcode - 安排时间的本地通知(每天早上 6 点)

标签 iphone objective-c xcode uilocalnotification

我正在使用 Xcode 4.3.2,如何在每天早上 6 点设置这个本地通知“dateToFire”?

-(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

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];

关于iphone - xcode - 安排时间的本地通知(每天早上 6 点),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10266336/

相关文章:

ios - Swift - 如何检查 map 注释(pin)是否在数组内?

iphone - 应用程序未通过代码设计验证?

objective-c - 禁用 Cocoa 应用程序的子像素抗锯齿

ios - 基于区域设置的设备日期格式

来自命令行的 Xcode "Build and Archive"

iphone - 如何在应用程序初始化之前获取 GPS 位置?

ios - CGImageRef 消耗大量内存

ios - 控制可以发送到不同类中的函数的参数值

ios - 当且仅当有新提交时,如何使 XCode 机器人在一天中的特定时间构建?

eclipse - Xcode 4 的 AVR?