gpt4 book ai didi

iphone - 单点触控中的 session 维护?

转载 作者:行者123 更新时间:2023-12-03 21:15:30 28 4
gpt4 key购买 nike

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

当应用程序进入前台时,我处理 session 维护以及 AppDelegate 中的 onResignInactive 事件。

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

最佳答案

那里有 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 有一个非常好的观点,即您不应该依赖客户端超时来进行 session 管理,但您也应该维护服务器状态和超时。

关于iphone - 单点触控中的 session 维护?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16331680/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com