2

My application based on banking domain, need session handling. When application is idle (without any touch events after application open) have to calculate the time in background.

I handle the session maintenance when application entered into foreground and onResignInactive events in AppDelegate.

I need to handle application when live. Please help me to figure out this feature

Sri
  • 456
  • 6
  • 22

2 Answers2

1

There's an answer for obj-C there: iOS perform action after period of inactivity (no user interaction)

So, at the application level, you keep a timer, and reset it on any event. Then you can do your business (like hiding any sensitive information) in the timer's handler.

Now, to the code.

First, you have to subclass UIApplication and make sure yours is instantiated:

//Main.cs
public class Application
{
    static void Main (string[] args)
    {
        //Here I specify the UIApplication name ("Application") in addition to the AppDelegate
        UIApplication.Main (args, "Application", "AppDelegate");
    }
}

//UIApplicationWithTimeout.cs

//The name should match with the one defined in Main.cs
[Register ("Application")]
public class UIApplicationWithTimeout : UIApplication
{
    const int TimeoutInSeconds = 60;
    NSTimer idleTimer;

    public override void SendEvent (UIEvent uievent)
    {
        base.SendEvent (uievent);

        if (idleTimer == null)
            ResetTimer ();

        var allTouches = uievent.AllTouches;
        if (allTouches != null && allTouches.Count > 0 && ((UITouch)allTouches.First ()).Phase == UITouchPhase.Began)
            ResetTimer ();
    }

    void ResetTimer ()
    {
        if (idleTimer != null)
            idleTimer.Invalidate ();
        idleTimer = NSTimer.CreateScheduledTimer (new TimeSpan (0, 0, TimeoutInSeconds), TimerExceeded);
    }

    void TimerExceeded ()
    {
        NSNotificationCenter.DefaultCenter.PostNotificationName ("timeoutNotification", null);
    }
}

This will ensure you have a timer running (caveat: the time starts only at the first event), that the timer will resets itself at any touch, and that a timeout will send a notification (named "timeoutNotification").

You now can listen to that notification and act on it (probably pushing a cover ViewController)

//AppDelegate.cs
[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{
    UIWindow window;
    testViewController viewController;

    public override bool FinishedLaunching (UIApplication app, NSDictionary options)
    {
        window = new UIWindow (UIScreen.MainScreen.Bounds);

        viewController = new testViewController ();
        window.RootViewController = viewController;
        window.MakeKeyAndVisible ();

        //Listen to notifications !
        NSNotificationCenter.DefaultCenter.AddObserver ("timeoutNotification", ApplicationTimeout);

        return true;
    }

    void ApplicationTimeout (NSNotification notification)
    {
        Console.WriteLine ("Timeout !!!");
        //push any viewcontroller
    }
}

@Jason had a very good point that you shouldn't rely on client timeout for your session management but you should probably maintain a server state - and timeout - as well.

Community
  • 1
  • 1
Stephane Delcroix
  • 15,420
  • 5
  • 52
  • 82
0

If you were writing this as a web app, you would put the session timeout logic on the server, not the client. I would take the same approach with a mobile client also - have the server manage the session and timeout.

Jason
  • 73,476
  • 14
  • 119
  • 139
  • As they're no easy way to push from the server, it's probably a better solution to have idle management on the client as well as on the server – Stephane Delcroix May 02 '13 at 13:45
  • With a banking app, any meaningful work you are going do will require server interaction, so notifying the user that their session has timed out whenever they try to do anything that requires a sync seems reasonable. Or the server could send a timeout notification. – Jason May 02 '13 at 13:49
  • He wants to do something if the application is live, but the user isn't doing anything. Like hiding the accounts balance if it's displayed e.g. There's no sync with server involved here. It's a lock from inside of the app. – Stephane Delcroix May 02 '13 at 13:51
  • @StephaneDelcroix you are rite i'm expecting exactly what you told – Sri May 03 '13 at 07:27