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