【发布时间】:2011-07-14 16:05:18
【问题描述】:
即使我的 iPhone 应用程序在后台,我如何使用 UILocalNotification 在每天晚上 8 点显示我的闹钟?
【问题讨论】:
标签: iphone alarm uilocalnotification
即使我的 iPhone 应用程序在后台,我如何使用 UILocalNotification 在每天晚上 8 点显示我的闹钟?
【问题讨论】:
标签: iphone alarm uilocalnotification
将fireDate 设置为晚上8 点,将repeatInterval 设置为NSDayCalendarUnit,并使用[[UIApplication sharedApplication] scheduleLocalNotification: myNotification]; 安排警报
【讨论】:
dasdom 答案是正确的。只是想补充一点,如果您的设备处于静音模式,警报将不会发出声音。这是 Apple 对 UILocalNotification 的限制。
【讨论】:
UILocalNotification *localNotification =[[UILocalNotification alloc]init];
NSCalendar *calendar=[NSCalendar currentCalendar];
[calendar setTimeZone:[NSTimeZone defaultTimeZone]];
unsigned currentFlag=NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit|NSWeekdayCalendarUnit;
NSDateComponents *comp=[calendar components:currentFlag fromDate:[NSDate date]];
comp.hour=8;
comp.minute=0;
comp.second=0;
NSDate *date=[[calendar dateFromComponents:comp]dateByAddingTimeInterval:0];
if (localNotification==nil) {
return;
}
localNotification.fireDate=date;
localNotification.timeZone=[NSTimeZone defaultTimeZone];
localNotification.repeatCalendar=[NSCalendar currentCalendar];
localNotification.alertBody=@"Good Morning dude..!";
localNotification.alertAction=@"Snooze";
localNotification.repeatInterval=NSDayCalendarUnit;
localNotification.soundName=@"goodmorning.caf";
[[UIApplication sharedApplication]scheduleLocalNotification:localNotification];
我希望这会对你有所帮助...!
【讨论】: