代码之家  ›  专栏  ›  技术社区  ›  Rakesh patanga onnoweb

如何实现iPhone本地应用程序的web用户会话概念以自动注销?

  •  1
  • Rakesh patanga onnoweb  · 技术社区  · 11 年前

    我想自动登录我的应用程序,如果用户在五分钟内没有与它交互,就像网站中的web用户会话一样。

    我在网上找遍了,找不到任何有用的东西。

    我是这方面的初学者。

    感谢任何帮助!

    1 回复  |  直到 11 年前
        1
  •  2
  •   Community Mohan Dere    9 年前

    伪代码:

    1. 运行一个计时器,在时间结束后(会话超时时间)调用一个方法。
    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
    

    进入“支持文件”文件夹,将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