Tuesday 15 February 2011

objective c - calling a method continuously even in background ios -



objective c - calling a method continuously even in background ios -

is there way phone call method continuously long app running in background or foreground.this method used check time , trigger notification according alter in time.

here method

-(void)checkdate { nsdate *today = [nsdate date]; nsstring *endtime =@"24:00"; nsdateformatter *dateformatter = [[nsdateformatter alloc] init]; [dateformatter setdateformat:@"hh:mm"]; nsstring *currenttime = [dateformatter stringfromdate:today]; nslog(@"############ current time %@",currenttime); if ([currenttime isequaltostring: endtime]) { nslog(@"notifiction fired"); [[nsnotificationcenter defaultcenter]postnotificationname:@"todayends" object:self]; } }

my requirement post nsnotification method irrespective apps state

as pointed out in comments, nstimer work if app in foreground. code execution apps in background quite tricky on ios.

there several possibilities accomplish this, uncertainty fit purpose here, examples are:

core location update: if using core location in app, app can receive updates on when gps position of device changes , gets chance perform operations in background based on new gps data voice on ip: the app provides voice-over-ip services. apps key automatically launched after scheme boot app can reestablish voip services. apps key allowed play background audio. (from apple docs) background fetches: background fetches can perform network requests on regular basis, still not able perform operations @ points in time can exactly specify, can rather tell ios want perform network requests in regular intervals , ios schedule requests you. here first-class read on background fetches.

for more info check apple docs on background execution!

for case, recommend take @ uilocalnotification. it's not 100% clear me trying achieve, uilocalnotification might way go you, sounds want notify user based on different times... uilocalnotification works in way can schedule notification while app in foreground (or background if using these background execution modes) notify user @ specific point in time.

hope helps!

ios objective-c uilocalnotification uibackgroundtask

No comments:

Post a Comment