如何将应用程序放到后台?

时间:2014-09-10 13:30:09

标签: ios

我的申请在30秒后无所事事应该到了后台。如果30秒后没有活动,我想将用户注销。它的应用程序包含用户界面。当用户想要回来时,他必须再次写入他的用户名和密码。我把我的代码放在下面:

Timer.m:
#define kApplicationTimeoutInMinutes 0.1
#define kApplicationDidTimeoutNotification @"AppTimeOut"
@interface Timer : UIApplication
{
    NSTimer     *myidleTimer;
}

-(void)resetIdleTimer;



Timer.h:

@implementation Timer

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

    if (!myidleTimer)
    {
        [self resetIdleTimer];
    }

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

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

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





AppDelegate.m:
@implementation AppDelegate

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

    return YES;
}

-(void)applicationDidTimeout:(NSNotification *) notif
{
    NSLog (@"time exceeded!!");

    //This is where storyboarding vs xib files comes in. Whichever view controller you want to revert back to, on your storyboard, make sure it is given the identifier that matches the following code. In my case, "mainView". My storyboard file is called MainStoryboard.storyboard, so make sure your file name matches the storyboardWithName property.
    UIViewController *viewController = [[UIStoryboard storyboardWithName:@"Main" bundle:NULL] instantiateViewControllerWithIdentifier:@"login"];

    [(UINavigationController *)self.window.rootViewController pushViewController:viewController animated:YES];
}

//metoda, która informuje o przejsciu z aktywnego do nieaktywnego stanu
- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}
//- (UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler:(void (^)(void))handler

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
  }

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

1 个答案:

答案 0 :(得分:1)

如果我理解正确,您需要与某些密码管理器类似的功能,这些密码管理器具有在一段时间后锁定自己的功能。

首先,让我们明确表示您无法将应用程序发送到iOS上的后台。这取决于用户。

您可以做的是在一段时间后锁定应用程序并显示用户和密码提示屏幕。要执行此操作,您需要一个计时器(NSTimer),该计时器会在用户的每个操作时重新启动。如果计时器在任何时候结束 - 30秒间隔过去,计时器将执行您的方法,您可以在其中显示具有用户和密码提示的模态视图控制器。这样,应用程序将保持锁定,直到用户输入用户名和密码。

检测上一步操作也可以通过多种方式完成:

  • 检测上次用户的触摸
  • 为所有应用操作添加几行代码
  • 混合导航方法
  • ...