简体   繁体   English

检查进入前台时iPhone App在后台的时间长度

[英]Checking how long iPhone App has been in background when coming into foreground

I have an iPhone App which allows users to log in and interact with a web service. 我有一个iPhone应用程序,允许用户登录和与Web服务交互。 I would like to have the ability for the user to be automatically logged out after a period of inactivity...more specifically if the App has been in the background for over a period of time (for example 1 hour). 我希望用户能够在一段时间不活动后自动注销...更具体地说,如果应用程序已经在后台工作超过一段时间(例如1小时)。

I would ideally like to run a check in the App Delegate method applicationWillEnterForeground that checks how long the app has been in the background and then if it has been over the time allowed, take them to the log in screen. 理想情况下,我想在App Delegate方法applicationWillEnterForeground中运行一个检查,检查应用程序在后台的时间长度,然后是否超过允许的时间,将它们带到登录屏幕。

How would I run this check in the above method? 我将如何在上述方法中运行此检查? I would appreciate some sample code. 我会很感激一些示例代码。

If this is not the best way to achieve my requirements then suggestions also welcome! 如果这不是达到我要求的最佳方式,那么建议也欢迎!

Many thanks in advance 提前谢谢了

Andy 安迪

You can use this: 你可以用这个:

- (void)applicationWillResignActive:(UIApplication *)application
{    
    NSDate *thisMagicMoment = [NSDate date];
    [[NSUserDefaults standardUserDefaults] setObject:thisMagicMoment forKey:@"lastMagicMoment"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    NSDate *thisMagicMoment = [NSDate date];
    NSDate *lastMagicMoment =  (NSDate *)[[NSUserDefaults standardUserDefaults] objectForKey:@"lastMagicMoment"];

    if (lastMagicMoment==nil)
    {
        NSLog (@"First launch!");
    }
    else
    {
        NSTimeInterval timeOfNoMagic = [thisMagicMoment timeIntervalSinceDate:lastMagicMoment]/3600.0;
        NSLog (@"Application was in background for %.1f hours", timeOfNoMagic);

        //do your stuff - treat NSTimeInterval as double

        if (timeOfNoMagic > 1.0)
        {
            //logout
        }
    }
}

Write the time and date to NSUserDefaults when the application enters the background or is quit. 当应用程序进入后台或退出时,将时间和日期写入NSUserDefaults。 Then read that time and date back from NSUserDefaults in the applicationWillEnterForeground and compare. 然后从applicationWillEnterForeground中的NSUserDefaults读取该时间和日期并进行比较。 If it is more than 1 hour (or whatever you set your timeout to be) then display the log in screen. 如果超过1小时(或者您设置的超时时间),则显示登录屏幕。

Save the time when the app enters background to NSUserDefaults. 节省应用程序进入NSUserDefaults后台的时间。 then read it when it re-enters foreground. 然后在重新进入前景时读取它。

In applicationWillEnterForeground if the time difference is over 1 hour you would set a flag or post a notification. 在applicationWillEnterForeground中,如果时差超过1小时,您将设置标志或发布通知。 ie you could have a method in your app delegate that logs the user out, or you could have a check for a flag in viewWillAppear which if necessary logs the user out. 即你可以在你的app委托中有一个记录用户的方法,或者你可以在viewWillAppear中检查一个标志,如果需要,可以将用户记录下来。

First set the timer in method applicationdidFinishLaunching: 首先在方法applicationdidFinishLaunching中设置计时器:

Now check the time in method applicationDidBecomeActive: this is called every time when user start the app again. 现在检查方法applicationDidBecomeActive中的时间:每次用户再次启动应用程序时都会调用此方法。

If time is greater than ex(1 hour) then reset timer and logout the user. 如果时间大于ex(1小时),则重置计时器并注销用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当应用程序进入前台时,如何自动停止所有后台任务 state - how can I automatically stop all background tasks when app is coming in foreground state 在iPhone中将应用程序背景设置为前景 - Make app background to foreground in iphone iPhone App 从后台进入前台时挂起/阻塞 UI 几秒钟 - iPhone App gets Hang/Blocks the UI for few seconds while coming in foreground from background 当iphone应用程序进入后台或前台状态时,如何保存数据? - How do you save data when the iphone app goes to background or foreground state? 再次打开已在iPhone中从后台杀死的应用时,调用哪种方法? - Which method is called when app which has been killed from background in iphone is opened again? 如何显示iPhone应用程序的背景图像或前景图像 - How to display background image or foreground image of an iphone app iPhone应用程序可以在后台运行多长时间? - How long can an iPhone app live in the background? 在后台检测到其他iPhone应用程序已被打开 - detect in background that other iPhone app has been opened iPhone:如何在选择按钮时避免背景颜色出现? - iPhone: How to avoid background color coming up when button is selected? iOS应用程序在后台显示通知,但在前台iPhone 4s和OS版本9.3不显示通知 - iOS app shows notifications when in background but not foreground iphone 4s & os version 9.3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM