【发布时间】:2013-05-02 06:25:41
【问题描述】:
我的应用程序基于银行域,需要会话处理。当应用程序空闲时(应用程序打开后没有任何触摸事件)必须在后台计算时间。
当应用程序进入前台和 AppDelegate 中的 onResignInactive 事件时,我处理会话维护。
我需要在现场处理应用程序。请帮我弄清楚这个功能
【问题讨论】:
标签: iphone session xamarin.ios
我的应用程序基于银行域,需要会话处理。当应用程序空闲时(应用程序打开后没有任何触摸事件)必须在后台计算时间。
当应用程序进入前台和 AppDelegate 中的 onResignInactive 事件时,我处理会话维护。
我需要在现场处理应用程序。请帮我弄清楚这个功能
【问题讨论】:
标签: iphone session xamarin.ios
那里有 obj-C 的答案:iOS perform action after period of inactivity (no user interaction)
因此,在应用程序级别,您可以保留一个计时器,并在发生任何事件时将其重置。然后您可以在计时器的处理程序中处理您的业务(例如隐藏任何敏感信息)。
现在,进入代码。
首先,您必须继承 UIApplication 并确保您的 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”)。
您现在可以收听该通知并对其采取行动(可能会推送一个封面 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 有一个很好的观点,即您不应该依赖客户端超时来进行会话管理,但您可能应该保持服务器状态 - 以及超时 - 也是如此。
【讨论】:
如果您将其编写为 Web 应用程序,您会将会话超时逻辑放在服务器上,而不是客户端上。我也会对移动客户端采用相同的方法 - 让服务器管理会话和超时。
【讨论】: