When the user hits the home button and closes the app, I need to log the user out, thus making them have to login each time they open the app. The login sessions are handled through drupal.
If I call this snippet of code while in the app it works, however, if I run it in the paused event of the app, I get an error logging out of drupal
var logout = Ti.Network.createHTTPClient({ onload:function(event) { Ti.API.info("Successfully logged out of drupal"); }, onerror: function(event) { Ti.API.info("Error logging out of drupal"); }, timeout:5000 }); logout.open('POST','http://mydomain.com/mobile/user/logout.json'); logout.send();
2 Answers
just logout the user on the resumed/resume event... this will force the same behavior, logging in whenever the app starts
Ok I've found a solution for Android. For iOS this is easy with the pause / resume events as Aaron stated.
For android it took some thinking. Hitting the back button on android is no problem because that takes me out of the app and I have the window set to exitOnClose so it logs me out anyway. I didn't realized that hitting the home button on android simple minimizes the app. The app is still running, technically. With that said, I made an idle timer using setInterval. After a certain amount of time of no input in the app, it will log the user out of drupal and close itself. This is what I came up. It still needs some tuning, but I think this is going to work for me for android
var idleCount = 0; var idleInterval = setInterval(timerIncrement, 1000); function timerIncrement() { idleCount = idleCount++; Ti.API.info(idleCount); if (idleCount > 9) { clearInterval(idleInterval); var logout = Ti.Network.createHTTPClient({ onload: function(event) { Ti.API.info('Successfully logged out of drupal'); win.close(); }, onerror: function(event) { Ti.API.info('Error logging out of drupal (Possibly, not logged in)'); win.close(); //Close the window anyway }, timeout:5000 }); logout.open('POST','http://mysite.com/mobile/user/logout.json'); logout.send(); } } win.addEventListener('touchend', function(event) { idleCount = 0; });The above will log me out after 10 seconds of idle time. Depending on your use, you can adjust the interval durations to use less resources and increase/decrease the idleCount max. I did 10 seconds as an example.
Your Answer
Think you can help? Login to answer this question!