iOS 每天提醒和每周提醒
iOS 每天提醒和每周提醒
//每天10点更新提示
-(void)alertByDay{
NSDate *date = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comp = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit |NSSecondCalendarUnit) fromDate:date];
int hour = [comp hour];
威尼斯wns.9778官网活动,int min = [comp minute];
int sec = [comp second];
long int delayTime;
if (hour < 10){//时间9点没到
delayTime = (21-hour) * 60 * 60 - min * 60 - sec;
}else {
delayTime = (24 -hour 10) * 60 * 60 - min * 60 - sec 24 * 60 * 60;
}
NSDate *dates = [date dateByAddingTimeInterval:delayTime];
UILocalNotification *localNoti = [[UILocalNotification alloc]init];
localNoti.alertAction = @"查看";
localNoti.alertBody = [NSString stringWithString:@"去看看软件是否更新"];
localNoti.fireDate=dates;
localNoti.repeatInterval=NSDayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:localNoti];
[localNoti release];
}
//NSWeekdayCalendarUnit 1 到7 1表示周日
-(void)alertByWeek{
NSDate *date = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comp = [calendar components:(NSWeekdayCalendarUnit| NSHourCalendarUnit |NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:date];
int weekDay=[comp weekday];
int hour = [comp hour];
int min = [comp minute];
int sec = [comp second];
long int delayTime;
BOOL figure=NO;
delayTime = (24 -hour 10) * 60 * 60 - min * 60 - sec 24 * 60 * 60;
if (weekDay==1) {//如果是周日
if (hour<=10) {//时间没到十点
delayTime = (21-hour) * 60 * 60 - min * 60 - sec;
figure=YES;
}
}
if (!figure) {
delayTime=(weekDay-1)*24*60*60 hour*60*60 min*60 sec;
}
//用一周时间 -已经度过时间 将要发生时间
delayTime=7*24*60*60-delayTime 10*60*60;
NSDate *dates = [date dateByAddingTimeInterval:delayTime];
UILocalNotification *localNoti = [[UILocalNotification alloc]init];
localNoti.alertAction = @"查看";
localNoti.alertBody = [NSString stringWithString:@"去看看软件是否更新"];
localNoti.fireDate=dates;
localNoti.repeatInterval=NSWeekCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:localNoti];
[localNoti release];
}
http://www.bkjia.com/IOSjc/978454.htmlwww.bkjia.comtruehttp://www.bkjia.com/IOSjc/978454.htmlTechArticleiOS 每天提醒和每周提醒 //每天10点更新提示 -(void)alertByDay{ NSDate *date = [NSDate date]; NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *co...
// 将通知添加到系统中
NSLog(@"设置的weekday = %d", newWeekDay);
NSDate *newFireDate = [[[NSCalendar currentCalendar] dateFromComponents:comps] dateByAddingTimeInterval:3600 * 24 * days];
// 设置重复间隔
notification.timeZone = [NSTimeZone defaultTimeZone]; // 使用本地时区
NSHourCalendarUnit |
NSDateComponents * components = [[NSCalendar currentCalendar] components:NSCalendarUnitWeekday|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecondfromDate:[NSDate date]];
NSQuarterCalendarUnit;
int temp = 0;
NSWeekdayOrdinalCalendarUnit |
int hour = [[timeArr firstObject] intValue];
[comps setHour:hour];
notification.repeatInterval = kCFCalendarUnitWeek;
days = (temp >= 0 ? temp : temp 7);
NSMonthCalendarUnit |
comps = [[NSCalendar currentCalendar] components:unitFlags fromDate:[NSDate date]];
NSDayCalendarUnit |
int days = 0;
int minute = [[timeArr lastObject] intValue];
notification.fireDate = newFireDate;
notification.soundName= UILocalNotificationDefaultSoundName;
本文由威尼斯wns.9778官网活动发布于计算机教程,转载请注明出处:iOS 每天提醒和每周提醒
关键词: