开发者

Is it legal to create a recursive background handler in iphone?(processing when app enters the background)

开发者 https://www.devze.com 2023-04-09 07:29 出处:网络
If I register an app for background app in did enter background and make a background handler with approximately 10开发者_如何学运维 mins to finish the task, after the time is over the handler will ca

If I register an app for background app in did enter background and make a background handler with approximately 10开发者_如何学运维 mins to finish the task, after the time is over the handler will call finishing block and in this block again create the background handler you will get again 10 mins and so on, in this way application will remain in background. I mean is it OK to do that....? or will apple object to this?


From the documentation:

Applications running background tasks have a finite amount of time in which to run them. (You can find out how much time is available using the backgroundTimeRemaining property.) If you do not call endBackgroundTask: for each task before time expires, the system kills the application.

So, no, you can't indefinitely run in the background.


Yeah thats correct, you cannot run infinitely in this way. But i have found one more trick, when applicatuon enters background, start playing an audio with 0 volume :-) Your app will never get killed.


There is a VoIP app, Media5, which can receive in background incoming calls using UDP sockets.

Developers said they used a "trick" to mantain the app active forever and I'd exclude the silent audio playing option. So the question is: what's that trick?

Also Bria can receive with UDP in background.


From Comment 20 at Issue 515: Background app support for iPhones with multitasking support:

I'm pretty sure that without playing continuously an audio file or logging GPS position you cannot keep alive UDP listening sockets in iOS 4.3+ (both in the main thread or in a secondary one). If you play an audio play with AVAudioPlayer (after initializing an AudioSession before) in a nsrunloop every 5 seconds, the main thread is kept active and moreover it's NOT necessary to declare the audio background support in info.plist.

I think this is the "trick" used by Media5 and Bria. I tried also creating a infinite TCP stream to the loopback interface declaring it as VoIP, just to see if also UDP socket is kept alive. The answer is no, only TCP sockets works while in background (and with screen locked), UDP is closed in the meanwhile and cannot listen for incoming calls anymore.

So the key point is that the main thread must be kept active... using a simple nstimer o infinite runloop alone is useless, since the watchdog kills the process after few seconds in background (except if you work in debug mode with GDB interface attached, it runs forever).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号