The implementation process of IOS7 Background Fetch

Source: Internet
Author: User

The iOS7 features a background fetch (Background fetch) that allows the user to execute part of the code before opening the app, such as preparing data, refreshing the UI, and so on. This is usually the maximum of 30 seconds.

Here are the basic steps to set up background fetch and record it.

1. Click the capabilities->>background Modes->>background fetch hook under target.

2. Set the minimum fetch interval

Can be directly inside the Appdelegate-(BOOL) Application: (uiapplication *) application Didfinishlaunchingwithoptions: (nsdictionary *) Launchoptions method Plus

[[UIApplication Sharedapplication] Setminimumbackgroundfetchinterval:uiapplicationbackgroundfetchintervalminimum ];

Uiapplicationbackgroundfetchintervalminimum is to tell the system to do background fetch as frequently as possible, but how often is this frequency? This is the system's own decision, developers will not know.

Even if you set the Minimumbackgroundfetchinterval very small, the system can only guarantee that the program will not be awakened during this time period, not that it will be awakened every time it takes so long. Therefore, developers should rationally set the time to wake up, according to the specific circumstances of the program to consider.

3. Next, you can implement the Proxy method for the Background fetch with block callback.

Here we can make a network request to see if the data is updated. However, because the timeout limit is 30 seconds, it is necessary to use the background Transfer service if this time is exceeded.

I wrote a local notification in this method to test.

//-------------------------------------------------------------#pragmaMark-background Fetch related delegate//-------------------------------------------------------------- (void) Application: (UIApplication *) application Performfetchwithcompletionhandler: (void(^) (Uibackgroundfetchresult)) completionhandler{NSLog (@"I am the legendary background Fetch??"); Uilocalnotification* Localnoti =[[Uilocalnotification alloc] init]; Localnoti.hasaction=YES; //swipe to ...Nsarray * Actionmsgs = @[@"look at a huge secret",@"See what the little buddies are doing.",@"See beautiful pictures",@"Collect Prizes",@"See what Hongo is doing.",@"pay for a dropbeacon",@"please vijay dinner ."]; Localnoti.alertaction= [actionmsgs objectatindex:arc4random ()%Actionmsgs.count]; Localnoti.alertbody=@"I am the legendary background Fetch??";    [[UIApplication sharedapplication] schedulelocalnotification:localnoti]; Completionhandler (Uibackgroundfetchresultnewdata);}

4. The next step is to test it.

There are two methods of testing.

The first option is to check the launch due to a background fetch event in Runscheme, as

The second option is to select the simulate Background fetch under Debug in the Xcode menu bar to switch the emulator directly to the Background fetch state, as

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.