《周期本地推送通知(闹钟功能)》由会员分享,可在线阅读,更多相关《周期本地推送通知(闹钟功能)(4页珍藏版)》请在金锄头文库上搜索。
1、初学iOS写些简单的笔记文章,希望对初学者有帮助; 下面是本地推送和周期设定的一些代码: 时间的编写与比较 /注意格式_weekdayUIApplication sharedApplication cancelAllLocalNotifications;NSArray *arr=NSArray allocinitWithObjects:“1“,“5“, nil;NSString*str=arr objectAtIndex:1;NSLog(“%d“,str intValue); /注意格式_optiontimeNSString *datestr=“2010-08-04 16:01:00“;NSD
2、ateFormatter *dateFormatter = NSDateFormatter alloc init;dateFormatter setDateFormat:“yyyy-MM-dd HH:mm:ss“;NSDate *date = dateFormatter dateFromString:datestr;NSArray *datearr=datestr componentsSeparatedByString:“ “;NSArray *timer=datearr1 componentsSeparatedByString:“:“;NSDate *now=NSDate date; /设置
3、时间与系统时间的比较if(now compare:date=NSOrderedSame) /系统时间等于设定时间NSCalendar *gregorian =NSCalendar currentCalendar;NSDateComponents * dateComps=gregorian components:NSWeekdayCalendarUnit fromDate:now;NSInteger datecount = dateComps weekday;NSDate *weekdaybegin = now addTimeInterval:-datecount*60*60*24;for(in
4、t i=0;iarr.count;i+)NSString *str=arr objectAtIndex:i;int weekday=str intValue;NSDate *date=weekdaybegin addTimeInterval:weekday*60*60*24;self creatmessage:date;else if (now compare:date=NSOrderedDescending) /系统时间晚于设定时间NSCalendar *gregorian =NSCalendar currentCalendar;NSDateComponents * dateComps=gr
5、egorian components:NSWeekdayCalendarUnit fromDate:now;NSInteger nowhour=dateComps hour;NSInteger nowmin=dateComps minute;NSInteger nowsecond=dateComps second;nowsecond=nowsecond+nowmin*60+nowhour*60*60;NSInteger optionhour=timer objectAtIndex:0 intValue;NSInteger optionmin=timer objectAtIndex:1 intV
6、alue;NSInteger optionsecond=timer objectAtIndex:2 intValue;optionsecond=optionsecond+optionmin*60+optionhour*60*60;NSInteger datecount = dateComps weekday;NSDate *weekdaybegin = now addTimeInterval:-datecount*60*60*24- nowsecond+optionsecond;for(int i=0;iarr.count;i+)NSString *str=arr objectAtIndex:
7、i;int weekday=str intValue;NSDate *date=weekdaybegin addTimeInterval:weekday*60*60*24;self creatmessage:date;else if (now compare:date=NSOrderedAscending) /系统时间早于设定时间NSCalendar *gregorian =NSCalendar currentCalendar;NSDateComponents * dateComps=gregorian components:NSWeekdayCalendarUnit fromDate:now
8、;NSInteger nowhour=dateComps hour;NSInteger nowmin=dateComps minute;NSInteger nowsecond=dateComps second;nowsecond=nowsecond+nowmin*60+nowhour*60*60;NSInteger optionhour=timer objectAtIndex:0 intValue;NSInteger optionmin=timer objectAtIndex:1 intValue;NSInteger optionsecond=timer objectAtIndex:2 int
9、Value;optionsecond=optionsecond+optionmin*60+optionhour*60*60;NSInteger datecount = dateComps weekday;NSDate *weekdaybegin = now addTimeInterval:-datecount*60*60*24- nowsecond+optionsecond;for(int i=0;iarr.count;i+)NSString *str=arr objectAtIndex:i;int weekday=str intValue;NSDate *date=weekdaybegin
10、addTimeInterval:weekday*60*60*24;self creatmessage:date; /推送通知设定的方法 -(void)creatmessage:(NSDate *)new UILocalNotification *notification=UILocalNotification allocinit;if(notification!=nil)NSDate *now=new;NSLog(“%“,now);notification.fireDate=now;notification.timeZone=NSTimeZone defaultTimeZone;notification.repeatInterval=kCFCalendarUnitWeek;notification.alertBody=“message“;notification.soundName=UILocalNotificationDefaultSoundName;notification.alertAction=NSLocalizedString(“wakeup“, nil); UIApplication sharedApplicationscheduleLocalNotification:notification;