Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在我的iphone应用程序中实现空闲时间_Iphone_Timer - Fatal编程技术网

在我的iphone应用程序中实现空闲时间

在我的iphone应用程序中实现空闲时间,iphone,timer,Iphone,Timer,我想在我的应用程序中实现一个功能,这样当用户在5分钟内不使用该应用程序时,该应用程序将从一开始运行,而不是从用户停止的地方运行 我发现plist属性“Application not run in background”(应用程序不在后台运行),但该函数允许应用程序始终从一开始就运行。有没有一种方法可以为这个plist属性设置计时器,或者在伪代码中执行类似的操作 更新: 所提到的方法是正确的。然而,我正在寻找一种解决方案,让应用程序在进入后台后注意空闲时间。(即按下主页按钮后)。希望你能帮助我

我想在我的应用程序中实现一个功能,这样当用户在5分钟内不使用该应用程序时,该应用程序将从一开始运行,而不是从用户停止的地方运行

我发现plist属性“Application not run in background”(应用程序不在后台运行),但该函数允许应用程序始终从一开始就运行。有没有一种方法可以为这个plist属性设置计时器,或者在伪代码中执行类似的操作


更新:

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


解决方案:

我找到了解决办法。首先,我要将日期保存在

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    //save date
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [[NSUserDefaults standardUserDefaults] setObject:NSDate.date forKey:@"date"];
    [defaults synchronize];
}
然后,当我返回应用程序时,我会将保存的日期与实际日期进行比较。如果时间间隔大于5分钟。应用程序将转到密码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];
        }
}

如果我的某个应用程序在后台运行超过一定时间,我会触发该应用程序的注销。为此,我的应用程序委托中有以下方法。某些调用依赖于我的重构库,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];
}

如果当你的应用程序运行时,use没有在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);
在我的博客上看到这篇文章-