gpt4 book ai didi

ios - 如何实现 iPhone 原生应用程序的 Web 用户 session 概念以自动注销?

转载 作者:行者123 更新时间:2023-11-29 10:40:08 25 4
gpt4 key购买 nike

我想自动注销我的应用程序,以防用户在五分钟内没有与其交互,就像网站中的 Web 用户 session 一样。

我在整个互联网上都试过了,找不到任何有用的东西。

我是这个开发的初学者。

感谢任何帮助!

最佳答案

伪代码:

  1. 运行计时器,在时间到( session 超时期限)后调用方法。
  2. 该方法应导航到登录事件
  3. 如果用户与该应用有任何互动,请重置计时器。

代码:

1) 创建一个新文件 -> Objective-C 类 -> 输入一个名称(在我的例子中是 SessionApplication)

2) 将子类更改为 UIApplication。您可能需要在子类字段中手动输入。

您现在应该拥有适当的 .h 和 .m 文件。

将 .h 文件更改为如下内容:

 #import <Foundation/Foundation.h>  

//the length of time before your application "times out". This number actually represents seconds, so we'll have to multiple it by 60 in the .m file
#define SessionTimeoutPeriodMins 5

//the notification your AppDelegate needs to watch for in order to know that it has indeed "timed out"
#define kapplicationDidSessionTimeoutNotification @"AppTimeOut"


@interface SessionApplication : UIApplication
{
NSTimer *mySessionTimer;
}

-(void)resetSessionTimer;

@end

将.m文件改成如下:

#import "SessionApplication.h"

@implementation SessionApplication

//here we are listening for any touch. If the screen receives touch, the timer is reset
-(void)sendEvent:(UIEvent *)event
{
[super sendEvent:event];

if (!mySessionTimer)
{
[self resetSessionTimer];
}

NSSet *allTouches = [event allTouches];
if ([allTouches count] > 0)
{
UITouchPhase phase = ((UITouch *)[allTouches anyObject]).phase;
if (phase == UITouchPhaseBegan)
{
[self resetSessionTimer];
}

}
}
//as labeled...reset the timer
-(void)resetSessionTimer
{
if (mySessionTimer)
{
[mySessionTimer invalidate];
}
//convert the wait period into minutes rather than seconds
int timeout = SessionTimeoutPeriodMins * 60;
mySessionTimer = [NSTimer scheduledTimerWithTimeInterval:timeout target:self selector:@selector(sessionTimedOut) userInfo:nil repeats:NO];

}
//if the timer reaches the limit as defined in SessionTimeoutPeriodMins, post this notification
-(void)sessionTimedOut
{
[[NSNotificationCenter defaultCenter] postNotificationName:kapplicationDidSessionTimeoutNotification object:nil];
}


@end

进入您的 Supporting Files 文件夹并将 main.m 更改为此(与以前版本的 XCode 不同):

#import <UIKit/UIKit.h> 
#import "AppDelegate.h"
#import "SessionApplication.h"

int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, NSStringFromClass([SessionApplication class]), NSStringFromClass([AppDelegate class]));
}
}

将剩余的代码写入您的 AppDelegate.m 文件中。我遗漏了与此过程无关的代码。 .h 文件无需更改。

#import "AppDelegate.h"
#import "SessionApplication.h"

@implementation AppDelegate

@synthesize window = _window;

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidSessionTimeout:) name:kapplicationDidSessionTimeoutNotification object:nil];

return YES;
}

-(void)applicationDidSessionTimeout:(NSNotification *) notif
{
NSLog (@“session timed out!!");
//Get the controller to login activity

}

Reference

Sample

关于ios - 如何实现 iPhone 原生应用程序的 Web 用户 session 概念以自动注销?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24972480/

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