Tell me how to organize the execution of tasks in the application at specified intervals, while the user can minimize the application?

  • How does this question differ from your previous one? - Max Mikheyenko
  • That question was aimed at performing a continuous operation in the background, and this one on periodic calls, as far as I understood, will not work for more than 10 minutes in the background? - user204104
  • Well, your options: 1) the application can ask the system to work on the background - no more than 10 minutes, while the system can kill your application at any time 2) you can declare yourself as a music player and work on the background as you want, but you should play what then sounds, otherwise the epl will not accept you 3) you can announce that your application wants to receive data from the server at certain intervals, then the system will wake you up to receive data on the background. - Max Mikheyenko
  • @MaxMikheyenko Thank you for the detailed answer! In order for me to receive data from the server, which object should I deal with, with background fetch, how did you advise on that question? Please add your answer to the question so that I can choose it with the right decision =) - user204104

1 answer 1

As I wrote in the comments, you need a background mode called background fetch.

This is done like this:

1) declare what you want to do background fetch in capabilities.

enter image description here

2) In the appDelegate declare method

- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { //ваш код загрузки здесь //сообщить системе об успешности загрузки через completionHandler - это делать обязательно BOOL downloadSuccessful = YES; if(downloadSuccessful) { completionHandler(UIBackgroundFetchResultNewData); } else { completionHandler(UIBackgroundFetchResultFailed); } } 

3) set how often you want to do it (usually done in didFinishLaunchingWithOptions:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum]; return YES; } 

Information taken from here .

  • Please tell me, if I have any operations on the device, without contacting the server, for example, I need to write time to the file every 5 minutes, how is it better to implement it? I tried using UILocalNotification and application_handleActionWithIdentifier, but for some reason I don’t enter the handle method, but didReceiveLocalNotification on ios8.0 + does not work at all - user204104
  • I'm not sure, but it seems to me without a hack. Something like a declare yourself a player and every 5 minutes to play some kind of sound, and at this time to write time. - Max Mikheyenko
  • but for it, most likely, I will receive a reject on the appstore? - user204104
  • I read somewhere an article that this is how someone implemented it. The question here is that if you showed up as a player and do not play music, then there will be a rejection. and if you play a sound, then there is no crime in it. - Max Mikheyenko
  • for example, a notification sound from an application, now a new question is being formed) how to wake up the application every 5 minutes using the player, make a short sound and make some records in the file? Should I issue a new question, or have I started too many similar questions? - user204104