2014-09-10 42 views
1

我的應用程序在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:. 
} 
+3

沒有辦法在iOS中以編程方式將應用程序發送給後臺。用戶必須通過點擊主頁按鈕來啓動此行爲,一次或兩次啓動應用程序切換器。 – rckoenes 2014-09-10 13:34:14

+1

@坦尼斯:目前還不清楚你真的在問什麼。如果30秒後沒有任何活動,您是否希望某些正在運行的進程閒置並將用戶註銷?請編輯您的問題,以包含您要做的更詳細的描述。 – 2014-09-10 15:35:33

回答

1

如果我理解正確的這個,你想有一個類似的功能,一些密碼管理器,它有一定的時間段後把自己鎖的功能。

首先,讓我們明確你不能將應用程序發送到iOS上的背景。這取決於用戶。

你可以做的是在一段時間後鎖定應用程序並顯示用戶和密碼提示畫面。爲此,您需要一個計時器(NSTimer),該計時器在用戶的每次操作中都會重新啓動。如果在任何時候計時器結束 - 間隔30秒,計時器將執行您的方法,您可以在其中顯示帶有用戶和密碼提示的模態視圖控制器。這樣,應用程序將保持鎖定狀態,直到用戶輸入用戶名和密碼。

檢測最後一個動作也可以通過多種方式來完成:

  • 最後檢測用戶觸摸
  • 添加幾行代碼和所有的程序操作
  • 混寫的導航方法
  • ...
+0

NSTimer是在我的應用程序,我從頁面獲取代碼:http://stackoverflow.com/questions/8085188/ios-perform-action-after-period-of-inactivity-no-user-interaction ...但我不知道我必須把這個方法放在instantiateViewControllerWithIdentifier中: UIViewController * controller = [[UIStoryboard storyboardWithName:@「MainStoryboard」bundle:NULL] instantiateViewControllerWithIdentifier:@「mainView」]; 我想這個問題: 由於未捕獲的異常'NSInvalidArgumentException'而終止應用程序,原因:'Storyboard不包含帶有標識符'login'的視圖控制器'' – Tannis 2014-09-11 08:28:10

+0

那麼,我們需要看一些代碼。按照您提供的鏈接回答問題,然後嘗試直接描述問題,這樣很難幫助您。 – Legoless 2014-09-11 08:29:48

+0

我把我的代碼放在我的問題中。 – Tannis 2014-09-11 08:36:33

相關問題