iOS development: Running in the background and keeping programs running in the background for a long time

Source: Internet
Author: User

First Part

1. First talk about the iOS app 5 states:

Stop running-the application has terminated or has not started.

Inactive-The application is in the foreground but no longer receives the event (for example, the user has locked the device while the app is active).

The active-app is in the "in use" state.

The background-app no longer appears on the screen, but it still executes the code.

Pending-app still resides memory but no longer executes code.

When the home button is pressed, the app moves from the active state to the background, and most apps usually turn from the background in a few seconds to hang.

When memory is tight, the iphone will first close those apps that hang.

Starting with IOS 4, the app can continue running for a short period of time (10 minutes) after retiring to the background;

2. You can also declare yourself as needing to run in the background, you can run without limit.

But limited to playing music, using GPS, VoIP,. It's worth mentioning that some applications play silent music in the background (auditing is not necessarily found) in order to achieve the purpose of running in the background.

IOS 5 starts with one more type: Download newspapers and magazines.

Then IOS 7 can download a variety of gadgets and timed crawls.

iOS 7 needs to be aware of the difference: before iOS 7, when the app enters the background to continue running, if the user locks the screen, then iOS waits for the app to finish running before it goes to sleep. On IOS 7, the system will go to sleep quickly, and those background apps will be paused. A background app can continue to run for a while if it receives an event that wakes up (such as timed events, pushes, location updates, and so on). Because the processing becomes intermittent, the download is also handled using nsurlsession (that is, the Background Transfer Service below).

3. In my opinion, Apple restricts the app to run in the background in order to more effectively use the current app hardware, otherwise, too many apps reside in the background, the phone resource consumption is a big problem.

Two. iOS7 backend interface mode provided later

1, Background Audio, this is the background of the sound, this is very early, but also the most used iOS device background application, call this interface can achieve background music playback.

2, location Services, which is the background of positioning, the system will have a unified page to manage.

3, VoIP, background voice service, similar to the Skype call application needs to call, can make voice calls in the background.

4, Newsstand, newspapers and magazines in the background automatically download updates, which can be automatically updated in real-time.

5, Background Task completion, this interface as early as iOS 4, it can be used by any type of app, but in the old system, the background limit of the interface run time is only 10 minutes, meaning when the application back to the background, The rear run can take only 10 minutes to go to sleep. IOS 7 has made a change to this interface, the original is 10 minutes in a row, that is, regardless of whether the user in 10 minutes to turn off the screen into hibernation, the app will still wait in the background for 10 minutes after the end of the launch, and the new improvement is that if you encounter a shutdown of the screen hibernation, the background running 10 minutes will be followed , the remaining background time will be counted until the user wakes the device again. This way the background is still running for 10 minutes, but not continuously, and the advantage of doing so is to save power.

If there are some dictionary applications with the background copy of the word selection function, in fact, it is the use of this interface, if the user opened the dictionary and launched, even if the screen is closed, but the dictionary is still running in the background, the power consumption is relatively large, on iOS 7, this problem can be resolved.

6, Remote Notification, this is the larger one of the improved interface, the past chat-type application to accept the push after the point in need to collect information, this situation in QQ, and other applications are most obvious. However, with this interface, the situation will no longer exist, and later push will be able to start the background task directly. It is important to note that remote notification supports silent notification (silent push) so that the Dropbox Sync app can silently sync in the background in the most energy-efficient mode, Similar to the cloth card comics This can also push the new chapter of the comic that is chasing and silently download in the background, until the download and then send a local push to users, users click to see no need to network.

7, Background Transfer Service, background upload download. iOS is closest to a traditional multi-tasking backend interface that can be called by any type of app without time constraints. Application scenarios include background uploading and downloading data, which makes it possible to update the data package in the background, upload video in the background, and so on, but as its name can only be used to handle the task of uploading and downloading such a transmission class, it is powerless to monitor such a background-like clipboard.

IOS 7 new background fetch, this background interface is mentioned on Apple WWDC 2013, it will automatically adjust according to user behavior to achieve the most efficient back-end mode, able to deal with not very timely information acquisition. For example, some background information updates for social and news applications, the iOS system intelligently assigns background acquisition frequency and startup time for each app based on the frequency, time, and current network and battery status of the app.

Three. Current social projects, how to use iOS background

1. Current Project Features:

A. In the case of long connection, the user is always online to receive the message instantly;

B. In the initialization of the connection, need to do a lot of processing, if often connected, it is necessary to power consumption, so try to in the background, not often disconnected after the connection;

2. Through the above analysis, re-election in what way to save the app background run

VoIP does not work;

Mute playback, do not clear this way, whether you can pass the audit;

VPNs push, the preferred way, (specific method: The user after logging in, send a device tokenid; When sending a message, the platform according to the other side is offline or online, to determine whether to send a push message)

Application of 3.background fetch in this project

Since the app takes a bit of time to initialize, the best way to do this is through background fetching, which guarantees a smooth user experience.

Part Two: Keep the program running in the background for a long time

iOS in order to make the device as far as possible to save power, reduce unnecessary overhead, keep the system smooth, so the back-end mechanism to use tombstone-style "fake backstage." In addition to the system of the official very few programs can be real backstage, the general developer developed the application background by the following restrictions:

1. After the user presses home, the app moves into the background, with 180s background time (iOS7) or 600s (iOS6) Running time can handle the background operation

2. When the 180S or 600S time passes, you can tell the system not to complete the task, need to continue to complete the application, the system approved the application can continue to run, but the total time will not exceed 10 minutes.

3. After 10 minutes, no matter how to apply to the system to continue the background, the system will force the suspension of the app, suspend all background operations, threads, until the user clicks the app again to continue to run.

Of course, iOS for special applications also retains some of the "real backstage" methods to extract the more commonly used:

1.VOIP

2. Location Services

3. Background download

4. Play silent Music in the background (easily influenced by phone or other program, so it is not considered)

5 .... more

Where VoIP needs to bind a socket link and declare to the system, the system will take over the connection in the background, once the remote data, your app will be awakened 10s (or less) time to process the data, more than the time or processing completed, the program continues to hibernate.

Backstage is now the introduction of the new API iOS7, online implementation of the code is relatively small, Bo master is not careful to find.

Since Bo's main app needs to be running in the background, sending messages to the server every once in a while to keep the account logged in, you must ensure that the app is not restricted by the system tombstone.

Bloggers first tried a lot of methods, including a friend sent a demo, every 180s background time expires to destroy themselves and then create a background task, but the actual test only 10 minutes. Finally, because the VoIP on the service side changes too large, time is too tight, so chose the location service method to maintain the background.

To start location services:

1. header files need to be introduced: #import

2. Defining Cllocationmanager * Locationmanager in APPDELEGATE.M; Easy control as a global variable

3. Initialize the location service at the beginning of the program startup:

1

2locationManager = [[Cllocationmanager alloc] init];

Locationmanager.delegate =self;//or Whatever class you has for managing location

4. Start the location service when the program is in the background

[Locationmanager Startupdatinglocation];(the first time to run this method, if the previous user has not used the app, it will pop up whether to allow location services, about whether the user is allowed, the following code is judged)

This way when the location service is available, the program will constantly refresh the background time, the actual test, found that the background 180s time is constantly refreshed, to achieve long-term background.

However, there are some problems with this, and on some machines, the location service may not be able to refresh the background time even if it is turned on, and it needs to completely terminate the program. Stability is not known for reasons of code or some mechanism of the system.



What's wrong with Wen/levan (Jane book author)
Original link: http://www.jianshu.com/p/174fd2673897
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".

iOS development: Running in the background and keeping programs running in the background for a long time

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.