IOS App Background Run

Source: Internet
Author: User

Another use of block is to allow the program to run longer in the background. In the past, when the app was left on the home button, the app only had a maximum of 5 seconds to do some saving or cleanup work. But the app can call the UIApplication beginBackgroundTaskWithExpirationHandler method, allowing the app to run for up to 10 minutes in the background. This time can be used to clean up the local cache, send statistics and other work.

The sample code that lets the program run long in the background is as follows:

AppDelegate.h file
@property (Assign, nonatomic) Uibackgroundtaskidentifier backgroundupdatetask;

APPDELEGATE.M file
-(void) Applicationdidenterbackground: (uiapplication *) application
{
[Self beingbackgroundupdatetask];
Add the code that you need to run long
[Self endbackgroundupdatetask];
}

-(void) Beingbackgroundupdatetask
{
Self.backgroundupdatetask = [[UIApplication sharedapplication] beginbackgroundtaskwithexpirationhandler:^{
[Self endbackgroundupdatetask];
}];
}

-(void) Endbackgroundupdatetask
{
[[UIApplication sharedapplication] endBackgroundTask:self.backgroundUpdateTask];
Self.backgroundupdatetask = Uibackgroundtaskinvalid;
}

IOS App Background 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.