Even when my iPhone application is in background, How can I use UILocalNotification to show my alram every 开发者_Python百科day at 8.00 PM?
Set the fireDate
to 8.00 PM and set the repeatInterval
to NSDayCalendarUnit
and schedule the alert with [[UIApplication sharedApplication] scheduleLocalNotification: myNotification];
dasdom answer is correct. just wanted to add to it that the alarm will not make a sound if your device is in silent mode. It is a restriction from Apple for 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];
I hope this will help you...!
精彩评论