ios - Can't schedule an UILocalNotification x days from now -
i've been trying way many hours without finding problem/solution. code supposed schedule notification x days comparing wd(the selected weekday) , item.itemdayname(int of current weekday). right code works when wd == 7, wd == 7 --> weekdays selected (wd 0-6 = sun-sat). appreciate feedback given, thank you.
- (void)schedulenotificationwithitem:(todoitem *)item andweekday:(int)wd{ nscalendar *calendar = [nscalendar autoupdatingcurrentcalendar]; nsdatecomponents *datecomps = [[nsdatecomponents alloc] init]; if (wd < 7) { [datecomps setyear:item.itemyear]; [datecomps setmonth:item.itemmonth]; if (item.itemdayname != (wd + 1)) { int result = (wd + 1) - item.itemdayname; result = result + item.itemday; [datecomps setday:result]; } else{ [datecomps setday:item.itemday]; } } else { [datecomps setyear:item.itemyear]; [datecomps setmonth:item.itemmonth]; [datecomps setday:item.itemday]; } [datecomps sethour:item.itemhour]; [datecomps setminute:item.itemminute]; nsdate *itemdate = [calendar datefromcomponents:datecomps]; uilocalnotification *localnotif = [[uilocalnotification alloc] init]; if (localnotif == nil) return; localnotif.firedate = itemdate; //localnotif.firedate = [itemdate datebyaddingtimeinterval:-(1*60)]; localnotif.timezone = [nstimezone defaulttimezone]; if (wd < 7) { [localnotif setrepeatinterval: nscalendarunitweekofmonth]; } else { [localnotif setrepeatinterval: nscalendarunitday]; //repeat daily } localnotif.alertbody = [nsstring stringwithformat:nslocalizedstring(@"time take %@!", nil), item.itemname]; localnotif.alertaction = nslocalizedstring(@"view details", nil); localnotif.alerttitle = nslocalizedstring(@"piller", nil); localnotif.soundname = uilocalnotificationdefaultsoundname; localnotif.applicationiconbadgenumber = 1; nsdictionary *infodict = [nsdictionary dictionarywithobject:item.itemname forkey:item.itemkeyname]; localnotif.userinfo = infodict; [[uiapplication sharedapplication] schedulelocalnotification:localnotif]; }
Comments
Post a Comment