简体   繁体   中英

is iOS background mode needed if app works when suspended?

I need my iPhone app to do some tasks in the background when it's suspended, but I don't need to do any tasks after it's turned off. Do I need to settle UIBackgroundMode ?

Yes, to execute code continuously in the background state, you need to register for a given UIBackgroundMode.

See here or here for a good source on this.

However if you just wish to perform a single background task (task completion), you don't need a background mode.

In response to your comment below, I will try to be clear:

In iOS, even in iOS 7, it's not possible to run arbitrary code when your application is in a background state indefinitely.

In simple English, - you can't just run any code you like in the background for as long as you like.

There are rules.

These rules have been relaxed somewhat in iOS 7, but it's still not totally unrestricted.

Consider your situation: You have a timer that wants to call a method on a continuous basis.

Now consider the UIBackgroundModes available (which allow you to run in the background in various situations):

audio - Only for audio based apps.

location - Only if you app is location aware, does specific location tasks

voip - VOIP (Skype etc)

fetch (Background fetch - a new iOS 7 API where the system gives you application moments to grab new content when it sees fit.

remote-notification - new in iOS 7, when the device receives a remote push notification with a certain payload, it will resume and execute a certain block of code.

newsstand-content - Only for newsstand apps

external-accessory - Only for external accessories to communicate with the device

bluetooth-peripheral Only for external BT accessories to communicate with the device (fitbit)

As you can see if you don't fall into one of these categories you can't use these modes. If you misuse the modes Apple won't approve your application.

Finally we have 'Background task completion'

This is a way to use a UIBackgroundTask to execute any code you want in the background! Including timers that call methods! - One problem though...

This is supposed to be for 'task completion' (Facebook uploads that aren't complete, saving or processing data that should be done before the app suspends even though the user pressed the home button).

Sounds good, but you can only run in the background using this method for a maximum of 10 minutes. If you go over this time limit iOS will immediately kill your application.

So as you can see, there is no way for your application to be approved on the store and constantly run in the background with a timer that calls your method.

Sorry about that.

If you want to learn more check this out.

使用位置服务并设置NO位置管理器的“ pausesLocationUpdatesAutomatically”属性。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM