IOS Background Task long-running background task

Source: Internet
Author: User

The project encountered the needs of the app into the background and can perform some tasks, so Google a bit, to organize a small record.

Everyone knows that the iOS system, all the apps into the background when the default is to pause all threads, wait until the home button to return to the foreground will not continue to execute. What about the scenes that need to be upload, download in the background, or do some calculations, and so on?

IOS7 introduced some new APIs, so that'sNSURLSession, just to replace the formerNSURLConnectionand born, in our usualAFNetworking 2.0Support (Afurlsessionmanager/afhttpsessionmanager) has also been added to theNSURLSessioncan be very convenient and quick to carry out some background network tasks, very powerful, the online material is also many, this article does not mention it, the latter may be separate to nsurlsession background task dedicated to write an article.

Today to introduce is long-running background task , "long-running background tasks", Chinese should be the name, when we need to enter the background does not suspend the program, you can give the system to say "I need to perform tasks in the background Oh ~", and then your App will not be suspended by the system, accurate is delayed pause, note that this is the global, It means that everything in the App will run normally, just like in the foreground. However, the system gives you this permission time is limited.

Begin

First we need to define a  uibackgroundtaskidentifier   Object, Oh, Command point is actually a nsuinteger ,

 Uibackgroundtaskidentifier MyTask;  

Then there are two ways to start a background task, all in  uiapplication  , you can see that the second method can pass a taskname parameter, and it is iOS7 new. (The original iOS4.0 supported ~)
is described in Apple's documentation:

marks the beginning of a new long-running background task.

- (UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler:(void(^)(void))handler  NS_AVAILABLE_IOS(4_0);- (UIBackgroundTaskIdentifier)beginBackgroundTaskWithName:(NSString *)taskName expirationHandler:(void(^)(void))handler NS_AVAILABLE_IOS(7_0);

OK, let's start a background task:

    myTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{// 后台任务到期执行,好像是10分钟}];

After executing the above sentence, your App will not be paused immediately when it enters the background again. But we can not abuse this, in order to be responsible for their own app, but also for the user's iPhone, the key is the misuse of the app Store audit that also can't get through ...

Stop

So, there is the relevant:

    [[UIApplication sharedApplication] endBackgroundTask: myTask];myTask = UIBackgroundTaskInvalid;

Other than thatUIApplicationTwo properties are also availablebackgroundTimeRemainingAndbackgroundRefreshStatus, can be self-study ...

Note: Regarding this time question, the author also somewhat puzzled, in the test time Expirationhandler will in 180 seconds, namely 3 minutes time trigger, but the background task still is continuing. Ask the great God for answers!

Reference article:

    • IOS APP Programming Guide-app states and multitasking
    • Hour 21:building Background-aware Applications
    • Continue uploading process in background IOS

IOS Background Task long-running background task

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.