Hi Titanium Folks,
I've been playing with Titanium for quite some time now but have just
recently embarked upon my first large project and have run into a bit
of a snag with background services not running when the user locks the
device from with in the app.
If the user hits the home button then everything is fine, but when the
device is locked and the app is in the foreground then the background
code never runs BUT I do see the pause event.
Here's the simplest implementation to show you what I'm doing.
app.js:
var w = Ti.UI.createWindow();
var bgsvc = Ti.App.iOS.registerBackgroundService({ url: '/bg.js' });
Ti.App.addEventListener('pause', function(e) {
Ti.API.debug("pause fired");
});
Ti.App.addEventListener('resumed', function(e) {
Ti.API.debug("resumed fired");
});
w.open();
bg.js:
Ti.API.debug("Hi from bg.js, about to loop forever")
function bgfunc() {
setTimeout(bgfunc, 5000);
Ti.API.debug("Hi from bgfunc");
}
bgfunc();
What I see when I press the home button:
[INFO] bgtest/1.0 (1.7.1.1293a6d)
[DEBUG] Analytics is enabled = YES
[DEBUG] loading: /Users/evan/Projects/Titanium/bgtest/Resources/
app.js, resource: Users/evan/Projects/Titanium/bgtest/Resources/app_js
[DEBUG] application booted in 28.014004 ms
[DEBUG] fire app event: pause
[DEBUG] pause fired
[DEBUG] loading: /Users/evan/Projects/Titanium/bgtest/Resources/bg.js,
resource: Users/evan/Projects/Titanium/bgtest/Resources/bg_js
[DEBUG] Hi from bg.js, about to loop forever
[DEBUG] Hi from bgfunc
[DEBUG] Hi from bgfunc
[DEBUG] fire app event: resumed
[DEBUG] resumed fired
... continues every 5 seconds ...
[DEBUG] fire app event: resumed
[DEBUG] resumed fired
What I see when the device locks:
[INFO] bgtest/1.0 (1.7.1.1293a6d)
[DEBUG] Analytics is enabled = YES
[DEBUG] loading: /Users/evan/Projects/Titanium/bgtest/Resources/
app.js, resource: Users/evan/Projects/Titanium/bgtest/Resources/app_js
[DEBUG] application booted in 27.069032 ms
[DEBUG] fire app event: pause
[DEBUG] pause fired
... wait 10 seconds, unlock ...
[DEBUG] fire app event: resumed
[DEBUG] resumed fired
I've been looking around for a couple days and have found the
following post on the developers site:
http://developer.appcelerator.com/question/116667/ios-background-geolocation-in-16
The link to 'github' in the second comment is specifically what I'm
interested in.
https://github.com/mpociot/titanium_mobile/commit/4853f091639e899545f9d1ef1416b1ca5baa0746
I haven't actually tried that diff yet as I didn't want to waste my
time if this is a known issue and the code in the diff doesn't
properly fix the issue.
Now to the questions. :)
Is this a known issue? Is the code posted in that diff a viable
solution that would be merged back upstream?
If so I'll fork from the titanium repo and re-build current with that
patch and happily await a updated binary distro in the future.
Thanks!
-E