iOS development easy to implement background tasks (such as background play music, timers, background positioning, etc.)

Source: Internet
Author: User
Tags set background vars

The maximum length of time to run, not to challenge the limit, how long it takes to keep the test.

Note: Test with a real machine

Directly on the code

In the Appdelegate. m, add some code actions when you enter the background

  1. /**
  2. * Called when the app enters the background
  3. */
  4. -(void) Applicationdidenterbackground: (uiapplication *) application
  5. {
  6. /** 
  7. * Status of App
  8. * 1. Death Status: No app opened
  9. * 2. Foreground operation status
  10. * 3. Background suspend state: Stop all animations, timers, multimedia, networking operations, it is difficult to do other operations
  11. * 4. Background running status
  12. */
  13. //to the operating system to apply for background running qualification, how long can be maintained, is uncertain
  14. Self.task = [Application beginbackgroundtaskwithexpirationhandler:^{
  15. When the background run time for the application has ended (expired), the block is called
  16. //
  17. Finish the mission.
  18. [Application EndBackgroundTask:self.task];
  19. //    }];
  20. __block uibackgroundtaskidentifier task = [Application beginbackgroundtaskwithexpirationhandler:^{
  21. //When the background run time of the application has ended (expired), the block will be called
  22. //Hurry to finish the task
  23. [Application Endbackgroundtask:task];
  24. }];
  25. /** 
  26. 1. Defining Variables Uibackgroundtaskidentifier Task
  27. 2. Execute the code on the right
  28. [Application beginbackgroundtaskwithexpirationhandler:^{
  29. When the background run time for the application has ended (expired), the block is called
  30. Finish the mission.
  31. [Application Endbackgroundtask:task];
  32. }];
  33. 3. Assign the return value of the right method to a task
  34. */
  35. //Set background mode in INFO.PLST: Required background modes = = APP plays audio or streams audio/video using AirPlay
  36. //engage in a 0kb MP3 file with no sound
  37. //Loop playback
  38. //Previous background mode has only 3 kinds
  39. //Maintain network connectivity
  40. //Multimedia Applications
  41. //VOIP: Internet telephony
  42. }

iOS development easy to implement background tasks (such as background play music, timers, background positioning, etc.)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.