ios - time comparison not working. What could be wrong? -
i worked on code perform pause notifications through sleep time, there missing can't quite figure out. be?
my .m
code:
- (ibaction)save:(id)sender { nsdateformatter *dateformatter = [[nsdateformatter alloc]init]; dateformatter.timezone=[nstimezone defaulttimezone]; dateformatter.timestyle=nsdateformattershortstyle; dateformatter.datestyle=nsdateformattershortstyle; nsstring *datetimestring=[dateformatter stringfromdate:_starttime.date]; nslog(@"start time %@",datetimestring); nsdateformatter *dateformatter2 = [[nsdateformatter alloc]init]; dateformatter2.timezone=[nstimezone defaulttimezone]; dateformatter2.timestyle=nsdateformattershortstyle; dateformatter2.datestyle=nsdateformattershortstyle; nsstring *datetimestring2=[dateformatter2 stringfromdate:_endtime.date]; nslog(@"end time %@",datetimestring2); if ([[nsdate date] isequaltodate:_starttime.date]) { nslog(@"currentdate equal starttime"); [[uiapplication sharedapplication] cancelalllocalnotifications]; nslog(@"sleep time silent started , notification cancelled"); } if ([[nsdate date] isequaltodate:_endtime.date]) { nslog(@"currentdate equal endtime"); [self pickerselectedrow]; nslog(@"time wake , notification rescheduled"); } }
when click on save
button receive out output means it's working fine, however, when time comes don't output saying notification has been cancelled or rescheduled, means it's not working!!!
output:
2013-05-03 03:04:57.481 xxx[10846:c07] start time 5/3/13, 3:06 2013-05-03 03:04:57.482 xxx[10846:c07] end time 5/3/13, 3:07
what missing?
also, work in background?
you need different compare. perhaps this: (all of following should in .m file)
- (ibaction)save:(id)sender { nsdateformatter *dateformatter = [[nsdateformatter alloc]init]; dateformatter.timezone=[nstimezone defaulttimezone]; dateformatter.timestyle=nsdateformattershortstyle; dateformatter.datestyle=nsdateformattershortstyle; nsstring *datetimestring=[dateformatter stringfromdate:_starttime]; nslog(@"start time %@",datetimestring); nsdateformatter *dateformatter2 = [[nsdateformatter alloc]init]; dateformatter2.timezone=[nstimezone defaulttimezone]; dateformatter2.timestyle=nsdateformattershortstyle; dateformatter2.datestyle=nsdateformattershortstyle; nsstring *datetimestring2=[dateformatter2 stringfromdate:_endtime]; nslog(@"end time %@",datetimestring2); if ([self date:[nsdate date] compareme:_starttime]) { nslog(@"currentdate equal starttime"); [[uiapplication sharedapplication] cancelalllocalnotifications]; nslog(@"sleep time silent started , notification cancelled"); } if ([self date:[nsdate date] compareme:_endtime]) { nslog(@"currentdate equal endtime"); [self pickerselectedrow]; nslog(@"time wake , notification rescheduled"); } } -(bool)date:(nsdate *)date1 compareme:(nsdate *)date2 { nscalendar *calendar = [nscalendar currentcalendar]; nsdatecomponents *date1componenets = [calendar components:nsdaycalendarunit|nsmonthcalendarunit|nsyearcalendarunit fromdate:date1]; nsdatecomponents *date2componenets = [calendar components:nsdaycalendarunit|nsmonthcalendarunit|nsyearcalendarunit fromdate:date2]; return [date1componenets year] == [date2componenets year]; }
if going compare dates lot, put comparing function in different file, such nsdate category, more complicated think need.
Comments
Post a Comment