单声道会话维护?

时间:2013-05-02 06:25:41

标签: iphone session xamarin.ios

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

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

我需要在现场处理申请。请帮我弄清楚这个功能

2 个答案:

答案 0 :(得分:1)

那里有obj-C的答案:iOS perform action after period of inactivity (no user interaction)

因此,在应用程序级别,您保留一个计时器,并在任何事件上重置它。然后,您可以在计时器的处理程序中开展业务(如隐藏任何敏感信息)。

现在,代码。

首先,您必须将UIApplication子类化,并确保您的实例化:

//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);
    }
}

这将确保你有一个计时器运行(警告:时间仅在第一个事件开始),计时器将在任何触摸时重置自己,并且超时将发送通知(名为“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有一个非常好的观点,你不应该依赖客户端超时进行会话管理,但你应该维护一个服务器状态 - 并且也应该超时。

答案 1 :(得分:0)

如果您将此作为Web应用程序编写,则会将会话超时逻辑放在服务器上,而不是客户端上。我也会采用与移动客户端相同的方法 - 让服务器管理会话并暂停。