单点触控中的会话维护? [英] Session maintenance in monotouch?

查看:51
本文介绍了单点触控中的会话维护?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我需要在直播时处理申请.请帮我弄清楚这个功能

解决方案

那里有obj-C的答案:

这将确保您正在运行计时器(提示:时间仅在第一个事件开始),计时器将在任何触摸时重置自身,并且超时将发送通知(名为"timeoutNotification"). /p>

您现在可以听取该通知并对其进行操作(可能是推一个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有一个很好的观点,即您不应该依赖客户端超时来进行会话管理,但是您可能还应该保持服务器状态-以及超时-.

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

解决方案

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.

这篇关于单点触控中的会话维护?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆