1

My application after 30 seconds of doing nothing should came to the background. If there's no activity after 30 seconds, I want to log the user out. It's application which contains user interface. When the user want to back he must write again his username and password. I put below my code:

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:.
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Tannis
  • 47
  • 1
  • 11
  • 3
    There is no way to send you app to the background programmatically in iOS. The user must initiate this behavior by clicking on the home button, either once or twice to start the app switcher. – rckoenes Sep 10 '14 at 13:34
  • 1
    @Tannis: It's unclear what you're really asking. Do you want some running processes to go idle and log the user out if there's no activity after 30 seconds? Please edit your question to include a much more detailed description of what you're trying to do. – Joshua Nozzi Sep 10 '14 at 15:35

1 Answers1

1

If I understand this correctly, you want a similar functionality to some password managers, which have a functionality of locking themselves after a certain period of time.

First, lets make clear that you cannot send the app to background on iOS. That is up to the user.

What you can do is lock the application after a certain period of time and display user and password prompt screen. To do this you need a timer (NSTimer), which gets restarted at every action by the user. If at any time timer gets to it's end - the 30 second interval passes, timer will execute your method, where you can display a modal view controller with user and password prompt. This way the app will stay locked until user enters username and password.

Detecting last action can also be done in multiple ways:

  • Detecting last user's touch
  • Adding few lines of code to all app actions
  • Swizzling navigation methods
  • ...
Legoless
  • 10,942
  • 7
  • 48
  • 68
  • NSTimer is in my app, I fetch the code from page: http://stackoverflow.com/questions/8085188/ios-perform-action-after-period-of-inactivity-no-user-interaction... but I don't know what i have to put in this method in instantiateViewControllerWithIdentifier: UIViewController *controller = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:NULL] instantiateViewControllerWithIdentifier:@"mainView"]; And i take that problem: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Storyboard doesn't contain a view controller with identifier 'login'' – Tannis Sep 11 '14 at 08:28
  • Well, we'd need to see some code. Follow the answer on the link you provided and after that try to describe the problem directly, it's really hard to help you this way. – Legoless Sep 11 '14 at 08:29