【问题标题】:Implement idle time within my iphone App在我的 iphone App 中实现空闲时间
【发布时间】:2017-04-01 01:32:44
【问题描述】:

我想在我的应用程序中实现一个功能,以便当用户 5 分钟不使用应用程序时,应用程序从头开始运行,而不是用户停止的地方。

我找到了 plist 属性“应用程序不在后台运行”,但这个功能让应用程序始终从一开始就运行。有没有办法可以为这个 plist 属性设置一个计时器或者用伪代码做类似的事情?


更新:

上面提到的方法都是正确的。但是,我正在寻找一种解决方案,让应用程序在应用程序进入后台后注意到空闲时间。 (即按下主页按钮后)。希望你能帮帮我


解决方案:

我找到了解决方案。首先,我将 NSDate 保存在

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    //save date
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [[NSUserDefaults standardUserDefaults] setObject:NSDate.date forKey:@"date"];
    [defaults synchronize];
}

然后,当我在应用程序中返回时,我会将保存的日期与实际日期进行比较。如果时间间隔大于 5 分钟。 App进入密码viewcontroller,强制用户重新登录!

    - (void)applicationDidBecomeActive:(UIApplication *)application
    {
        //calculate difference in time
        NSDate *time = [[NSUserDefaults standardUserDefaults] objectForKey:@"date"];

        NSTimeInterval timeInterval = [[NSDate date] timeIntervalSinceDate:time];

        if(timeInterval >= 300){

            Password *vc = [[Password alloc] init];
            self.window.rootViewController = vc;
            [vc release];

            [self.window makeKeyAndVisible];
        }
}

【问题讨论】:

    标签: iphone timer


    【解决方案1】:

    如果在你的应用程序运行时使用没有在 iPad 上触摸意味着他没有在使用你的应用程序对吗?

    然后您可以按照下面的代码检查空闲时间...(我从我的博客文章中粘贴此代码)

    第 1 步 – 在您的项目中添加一个类 (IdleTimeCheck),它是 UIApplication 的子类。在实现文件中,重写 sendEvent: 方法,如下所示:

    - (void)sendEvent:(UIEvent *)event 
    {
        [super sendEvent:event];
    
        // Only want to reset the timer on a Began touch or an Ended touch, to reduce the number of timer resets.
        NSSet *allTouches = [event allTouches];
        if ([allTouches count] > 0) 
        {
            // allTouches count only ever seems to be 1, so anyObject works here.
            UITouchPhase phase = ((UITouch *)[allTouches anyObject]).phase;
            if (phase == UITouchPhaseBegan || phase == UITouchPhaseEnded)
                [self resetIdleTimer];
        }
    }
    
    - (void)resetIdleTimer 
    {
        if (idleTimer) {
            [idleTimer invalidate];
            [idleTimer release];
        }
    
        idleTimer = [[NSTimer scheduledTimerWithTimeInterval:maxIdleTime target:self selector:@selector(idleTimerExceeded) userInfo:nil repeats:NO] retain];
    }
    
    - (void)idleTimerExceeded {
        NSLog(@"idle time exceeded");
        //write logic to go to start page again
    }
    

    其中 maxIdleTime 和 idleTimer 是实例变量。

    第 2 步 - 修改 main.m 文件中的 UIApplicationMain 函数,以使用 UIApplication 子类作为主体类。

    int retVal = UIApplicationMain(argc, argv, @"IdleTimeCheck",nil);
    

    在我的博客上查看这篇文章 - http://www.makebetterthings.com/iphone/detecting-user-inactivityidle-time-since-last-touch-on-screen/

    【讨论】:

      【解决方案2】:

      如果应用程序在后台运行超过一定时间,我会在其中一个应用程序中触发注销。为此,我的应用程序委托中有以下方法。某些调用依赖于我的重构库 es_ios_utils(不是真正必需的),并且我的 UserDefaults 模型的代码不包括在内,但这应该会给你一个想法:

      -(void)applicationDidEnterBackground:(UIApplication*)application
      {
          UserDefaults.instance.enteredBackgroundAt = NSDate.date;
      }
      
      -(void)applicationDidBecomeActive:(UIApplication*)application
      {
          if([UserDefaults.instance.enteredBackgroundAt dateByAddingMinutes:20].isPast)
              [self logOut];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-07
        • 2012-08-09
        • 2012-11-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多