iOS development-After the application of the background, quietly crawl data ~ ~

Source: Internet
Author: User

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 ~ ~

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.