setTimeout with BackgroundService

You must Login before you can answer or comment on any questions.

I do not understand why the timer runs in the background for 9 minutes but when I rule the timer to 15 minutes it no longer works even in the background?

app.js

var service;
 
Ti.App.addEventListener('pause',function(e){
 
 
        service = Ti.App.iOS.registerBackgroundService({url:'bg.js'}); 
});

bg.js

var Min = 15;
var MiliSec = (Min*60)*1000);
 
setTimeout(function(){
 
 var xhr_power = Ti.Network.createHTTPClient();
     xhr_power.open("GET","http://www.monsite.fr");
 
        xhr_power.onload = function()
        {
            try
            {
                Ti.API.info('ok');
            }
            catch(E)
            {
                Ti.API.info('Error : '+E);
            }
        };
        // Get the data
     xhr_power.send();
 
}, MiliSec;

11 Answers

The app on iOs gets killed after 10 minutes when put in the background unless you set the background mode, but in this case restrictions apply. http://developer.apple.com/library/ios/#documentation/general/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html

scroll to UIBackgroundModes

?I have a solution that works but it is not the best, I created an audio object then I start reading the application is in the background, I created an empty sound is read loop which allows to have my code that works in the background :

var player = Ti.Media.createSound({url:"sons.mp3", looping:true, allowBackground:true});
 
Ti.Media.defaultAudioSessionMode = Ti.Media.AUDIO_SESSION_MODE_PLAYBACK;

Because, so far as I know, an app cannot stay more than 10 minutes in background before iOS somehow terminates it.

I tried with

<key>UIBackgroundModes</key>
<array>
    <string>newsstand-content</string>
</array>
but I still have the same problem you advise me what to UIBackgroundModes Value ? Thank you for answer

— answered 8 months ago by Flavo Mario
answer permalink
3 Comments
  • Try with audio or location, but if you don't really use this Apple will reject your app.

    — commented 8 months ago by Dan Tamas

  • I tried with both values ??'audio' and 'location' but I still have the same problem my code in bg.js not running, I do not know what to do ...

    — commented 8 months ago by Flavo Mario

  • I've the same issue. My newest idea to solve this, is delete my custom info.plist file in the project folder and add the background modes to the tiapp.xml directly. I read Titanium will generate the plist-file for you. I hope that works. In the example I saw, you should add this to the tiapp.xml:

    <backgroundModes>
        <mode>location</mode>
    </backgroundModes>
    <requires>
        <feature>gps</feature>
        <feature>location-services</feature>
    </requires>
    (for location-backgrund-events)

    What you should know about this background-services: Apple doesn't allow you to run any code in the background, except for explicit services from Apple (location, audio, etc). When you note one of this in you tiapp.xml as BackgroundMode you have to use functionality like this. Otherwise Apple will reject your app.

    — commented 7 months ago by Sirik Loosman

I think that's an option, but Apple wouldn't accept that I think, too... Or do you have a productive application in the AppStore which runs with this solution in the background?

Okay, fresh! :) But i mean, your application must something do with audio in the background. Otherwise apple will reject it, you app use the audio-function for background and apple allow that it this case. But as example for an tracking-app (should use "location" BackgroundMode) I can't put "audio" in the tiapp.xml for background-use.

I then had the chance because my application has passed the validation :)

Lucky guy. :) One question: How did you create the "empty" mp3-file?

I created a silent mp3 file with garage band, if you want I send you by mail

Your Answer

Think you can help? Login to answer this question!