making a network call during the paused event in iOS

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

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();

— asked 10 months ago by Ronnie Swietek
1 Comment
  • leave a comment next time you want to down vote my legitimate question...coward

    — commented 10 months ago by Ronnie Swietek

2 Answers

just logout the user on the resumed/resume event... this will force the same behavior, logging in whenever the app starts

— answered 10 months ago by Aaron Saunders
answer permalink
5 Comments
  • Only problem with this is our tracking will see that they are logged in from the time they open it to the time the reopen it

    — commented 10 months ago by Ronnie Swietek

  • Ok, I am using the resumed event for now...I have to move on. I've searched and cannot find anything for when android users hit their Home button. What do I do in this case?

    — commented 10 months ago by Ronnie Swietek

  • I would look to see of there is something you can check on the currentActivity object in Android... just a guess

    — commented 9 months ago by Aaron Saunders

  • Show 2 more comments

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!