Latheesan
Latheesan

Reputation: 24116

Detect user inactivity in titanium appcelerator iOS application

I am working on an app that allows user to login with your username & password and do some work in the app.

However, if the user has been idle / not using the app for 5 minutes, I want to "lock" my logged in user's session and re-direct them to the login page.

So, the way to do this is to start a global timer (setInterval) when they successfully logged in to call a session validate function. Then subscribe to any touch event in the app to reset the timer. If the timer has elapsed > 300 seconds, lock user.

I was googling along those lines and found the XCode Objective-C equivalent: https://stackoverflow.com/a/309535

Is there way to achieve something like this in titanium appcelerator? i.e. listen to ALL events on the whole app, when they stop occurring for 5 minutes; i.e. user is idle on app, lock the app (redirect to a different view for e.g. login).

I tried to listen to application wide touchend events to reset the timer and it did not work:

// alloy.js
Ti.App.addEventListener('touchend', function(e){
    console.log('### touchend ###');
});

Upvotes: 1

Views: 610

Answers (2)

Dawson Toth
Dawson Toth

Reputation: 5680

If you add a touchstart listener to your windows, you will receive most of the touch events. Some views, like scroll views (and by extension table views and list views) won't give you event when you scroll them (they cancel propagation). But if that's critical, you can add a listener to those too, as well as to anywhere that you don't propagate events.

I'd suggest you add a single module to your codebase, something like "activity-tracker.js" in Resources:

var timeoutID;
exports.didActivity = function() {
    if (timeoutID) {
        clearTimeout(timeoutID);
    }
    timeoutID = setTimeout(userIsInactive, 5 * 60 * 1000);
};

function userIsInactive() {
     alert('WHY YOU LEAVE ME?! COME BACK!');
}

Then in your app.js with your main window (and any other windows you define):

var win = Ti.UI.createWindow();
win.addEventListener('touchstart', require('activity-tracker').didActivity);
win.open();

And if you want a scroll view to flag them as active:

scroll.addEventListener('scroll', require('activity-tracker').didActivity);

Get the picture?

Upvotes: 1

Sebastian
Sebastian

Reputation: 625

I think you have to fire the touchEvent of every single window to an Ti.App.addEventlistener to exceed your timeout for the user

Upvotes: 1

Related Questions