iphone - Can we call the method after the application has been minimized? -


ios

can call method after application has been minimized?

for example, 5 seconds after called applicationdidenterbackground:.

i use code, test method don't call

- (void)test {     printf("test called!"); }  - (void)applicationdidenterbackground:(uiapplication *)application {     [self performselector:@selector(test) withobject:nil afterdelay:5.0]; } 

you can use background task apis call method after you've been backgrounded (as long task doesn't take long - ~10 mins max allowed time).

ios doesn't let timers fire when app backgrounded, i've found dispatching background thread before app backgrounded, putting thread sleep, has same effect timer.

put following code in app delegate's - (void)applicationwillresignactive:(uiapplication *)application method:

// dispatch background queue dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_background, 0), ^{      // tell system want start background task     uibackgroundtaskidentifier taskid = [[uiapplication sharedapplication] beginbackgroundtaskwithexpirationhandler:^{         // cleanup before system kills app     }];      // sleep block 5 seconds     [nsthread sleepfortimeinterval:5.0];      // call method if app backgrounded (and not inactive)     if (application.applicationstate == uiapplicationstatebackground)         [self performselector:@selector(test)];  // or, call [self test]; here      // tell system task has ended.     if (taskid != uibackgroundtaskinvalid) {         [[uiapplication sharedapplication] endbackgroundtask:taskid];     }  }); 

Comments