When we use an app, when we press the program into the background, we want it to be able to fetch some data from the server, similar to Weibo, QQ, these programs are pressed backstage
After that, we can still see the unread number displayed on the icon. But iOS is a pseudo-multitasking operating system.
After we have the program in the background, about 1 minutes, the program will be closed. You won't be able to crawl the data. A long time ago the practice is: after the background, play a no sound
Audio to ensure that the program survives. Then Apple soon discovered the method and banned it. After we have submitted the review in this way, we will generally be dismissed by Apple. Of course, Apple
Still very intimate to provide me with the corresponding solution. (Use Swift Demo.) OC method is consistent with Swift)
The first step, set
The second step, in the Appdelegate
func Application (application: uiapplication, didfinishlaunchingwithoptions launchoptions: [ NSObject: anyobject], Bool
Set minimum background fetch time in
1 // set minimum background fetch interval 2 Application.setminimumbackgroundfetchinterval (Uiapplicationbackgroundfetchintervalminimum)
The third step is to implement a proxy method in Appdelegate and write the code
1 ///Proxy method for fetching data in background2Func application (application:uiapplication, Performfetchwithcompletionhandler Completionhandler: ( Uibackgroundfetchresult)Void) { - the //request data that needs to be obtained -Hjcstatus.loadunreadstatus {(count), ()inch -Application.applicationiconbadgenumber =Count - + ifCount >0 {
Callback to tell the result of this network request - Completionhandler (uibackgroundfetchresult.newdata) +}Else{
Callback to tell the result of this network request A Completionhandler (uibackgroundfetchresult.nodata) at } - } -}
In this step, there are a few points to note
1. can not do too much data volume of the request, to try to small
2. The network request, after the end, must pass the callback method, tells the system the result of this network request
- with Data
- No data
- Error
According to the report, the system can automatically dispatch the background of the network data capture frequency, specific scheduling time, the programmer can not intervene
3. If the background crawl data is too large, while the network ' timeout ' frequent, the entire background task will be suspended by the system, no longer dispatched
The fourth step, set in the
iOS development-After the application of the background, quietly crawl data ~ ~