按照本教程,我已将我的 iPad 应用程序设置为半小时后超时。
这可以通过模拟器上的 Xcode 或连接到 Mac 的 iPad 完美运行。但是,如果我断开 iPad 与 Mac 的连接并重新登录我的应用程序,它就不再超时。
我希望有人能够解释为什么会发生这种情况。
AppDelegate.m
-(void)applicationDidTimeout:(NSNotification *) notif
{
NSLog (@"time exceeded!!");
UIViewController *controller = [[UIStoryboard storyboardWithName:@"MainStoryboard_iPad" bundle:NULL] instantiateViewControllerWithIdentifier:@"mainView"];
[(UINavigationController *)self.window.rootViewController pushViewController:controller animated:YES];
}
TIMERUIApplication.h
@interface TIMERUIApplication : UIApplication
{
NSTimer *myidleTimer;
}
-(void)resetIdleTimer;
TIMERUIApplication.m
-(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];
}
}
}
-(void)resetIdleTimer
{
if (myidleTimer)
{
[myidleTimer invalidate];
}
int timeout = kApplicationTimeoutInMinutes * 60;
myidleTimer = [NSTimer scheduledTimerWithTimeInterval:timeout target:self selector:@selector(idleTimerExceeded) userInfo:nil repeats:NO];
}
-(void)idleTimerExceeded
{
isLoggedIn = FALSE;
numberOfFIlesAlreadyDownloaded = 0;
numberOfFilesToBeDownloaded = 0;
[[NSNotificationCenter defaultCenter] postNotificationName:kApplicationDidTimeoutNotification object:nil];
}