How iOS daemon continues to run

Source: Internet
Author: User

The resources for iOS systems are limited, and applications are not the same in the foreground and in the background. In the background, the program is subject to many limitations of the system, which can improve battery usage and user experience. But there are a lot of social software, if because it can not be refreshed in the background to the new data, then this affects the user experience, the following is the program back to the background can continue to run the system is not purged by the method.

In the Appdelegate

-(void) Applicationdidenterbackground: (uiapplication *) application{

Uibackgroundtaskidentifier id=[application beginbackgroundtaskwithexpirationhandler:^{

Called when the background task ends

[Application Endbackgroundtask:id];

This function is called when the program enters the background, we start a background task in this function is only the time is uncertain, the priority is relatively low, if the system memory is not enough, it will be shut down

When this step fails, it is necessary to play the killer, the Apple system on the background of the priority of the program has a clear division, if you are playing music, then your priority is very high, the system generally will not clear;

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchoptions{

Play music in the background on a real machine, you need to set up an audio session

Avaudiosession *session=[avaudiosession Sharedinstance]; This is a singleton mode and can be used globally

Set session type (background playback)

[Session Setcategory:avaudiosessioncategoryplayback Error:nil];

Activated

[Session Setactive:yes Error:nil];

}

When it's about to lose focus

-(void) Applicationwillresignactive: (uiapplication *) Application {

Nsurl *url=[[nsbundle mainbundle]urlforresource:@ "Aaa.mp3" Withextension:nil]; Aaa.mp3 is a silent piece of music

Avaudioplayer *player=[[avaudioplayer Alloc]initwithcontentsofurl:url Error:nil];

[Player Preparetoplay];

Keep Looping

Player.numberofloops=-1;

[Player play];

_player=player;

}

Using the above two steps can successfully deceive the Apple system, our program can be running in the background.

How iOS daemon continues to run

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.