2

我的应用程序基于银行域,需要会话处理。当应用程序空闲时(应用程序打开后没有任何触摸事件)必须在后台计算时间。

当应用程序进入前台和 AppDelegate 中的 onResignInactive 事件时,我处理会话维护。

我需要在现场处理应用程序。请帮我弄清楚这个功能

4

2 回答 2

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.

于 2013-05-02T13:49:10.853 回答
0

如果您将其编写为 Web 应用程序,您会将会话超时逻辑放在服务器上,而不是客户端上。我也会对移动客户端采取同样的方法——让服务器管理会话和超时。

于 2013-05-02T12:53:44.987 回答