Apply For more background time for iOS development tutorials

Source: Internet
Author: User

During IOS application and game development, we often need to perform operations when users switch to the background, such as clearing the memory or saving user data. These operations are generally performed in

Applicationdidenterbackground of appdelagate :(Uiapplication*) In the application (of course, we can alsoReceive applicationsProgramThe notification uiapplicationdidenterbackgroundnotification is sent to trigger processing.). However, these work systems only allocate five seconds for processing. Otherwise, the system will force the application to exit. Therefore, in many cases, we need to apply for more time to process related background operations.

Now let's take a look at how to apply for more background time.CodeAs follows:

-(Void) Applicationdidenterbackground :(Uiapplication*) Application

{

Uiapplication* APP = [Uiapplicationsharedapplication];

//A background task identifier 

_ BlockUibackgroundtaskidentifierTaskid;

//Apply For more background running time

Taskid = [AppBeginbackgroundtaskwithexpirationhandler: ^ (Void){

//Execution beyond the extended system time

Nslog(@ "Background task ran out of time and was termined .");

[AppEndbackgroundtask: Taskid];

}];

//Application failed

If(Taskid =Uibackgroundtaskinvalid){

Nslog(@ "Fail to start background task! ");

Return;

}

Nslog(@ "Starting background task with % F seconds remaining", App.Backgroundtimeremaining);

While(True){

[Nsthreadsleepfortimeinterval: 1];

Nslog(@"Ding in the background");

}

}

I think the code has been commented out clearly. when running the code, we will find that the system gives us an extended period of nearly 600 s. Of course, this is enough for us to clean up memory and save user information.

 

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.