The answers from Gabriele and tdevoy clearly states why it is not possible. So I'm not repeating the same thing, instead if you cant find out how to do it, this is how you can try:
In your viewDidLoad
, store the logged in time in NSUserDefaults
like this
NSDate *currentDate= [NSDate date];
[[NSUserDefaults standardUserDefaults] setObject:currentDate forKey:@"loggedInTime"];
Now in your applicationDidBecomeActive
delegate method,
NSDate *loggedInTime = (NSDate *)[[NSUserDefaults standardUserDefaults] objectForKey:@"loggedInTime"];
NSTimeInterval timeSpentInApp = [[NSDate date] timeIntervalSinceDate:loggedInTime];
//if this timeSpentInApp is greater than 43200, then you can call logout.
//(Make sure after relogin, the loggedInTime value was updated again.)
But, if the user continuously uses the app for 12 hrs, then you should consider using your code snippet too, as this will call only when the app comes from background.