ios - How to put application to the background? -


my application after 30 seconds of doing nothing should came background. if there's no activity after 30 seconds, want log user out. it's application contains user interface. when user want must write again username , password. put below code:

timer.m: #define kapplicationtimeoutinminutes 0.1 #define kapplicationdidtimeoutnotification @"apptimeout" @interface timer : uiapplication {     nstimer     *myidletimer; }  -(void)resetidletimer;    timer.h:  @implementation timer  //here listening touch. if screen receives touch, timer 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 timer -(void)resetidletimer {     if (myidletimer)     {         [myidletimer invalidate];     }     //convert wait period minutes rather seconds     int timeout = kapplicationtimeoutinminutes * 60;     myidletimer = [nstimer scheduledtimerwithtimeinterval:timeout target:self selector:@selector(idletimerexceeded) userinfo:nil repeats:no];  } //if timer reaches limit defined in kapplicationtimeoutinminutes, post 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 storyboarding vs xib files comes in. whichever view controller want revert to, on storyboard, make sure given identifier matches following code. in case, "mainview". storyboard file called mainstoryboard.storyboard, make sure file name matches 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 nieaktywnego stanu - (void)applicationwillresignactive:(uiapplication *)application {     // sent when application move active inactive state. can occur types of temporary interruptions (such incoming phone call or sms message) or when user quits application , begins transition background state.     // use method pause ongoing tasks, disable timers, , throttle down opengl es frame rates. games should use method pause game.  } //- (uibackgroundtaskidentifier)beginbackgroundtaskwithexpirationhandler:(void (^)(void))handler  - (void)applicationdidenterbackground:(uiapplication *)application {     // use method release shared resources, save user data, invalidate timers, , store enough application state information restore application current state in case terminated later.     // if application supports background execution, method called instead of applicationwillterminate: when user quits.  }  - (void)applicationwillenterforeground:(uiapplication *)application {         // called part of transition background inactive state; here can undo many of changes made on entering background. }  - (void)applicationdidbecomeactive:(uiapplication *)application {     // restart tasks paused (or not yet started) while application inactive. if application in background, optionally refresh user interface.   }  - (void)applicationwillterminate:(uiapplication *)application {     // called when application terminate. save data if appropriate. see applicationdidenterbackground:. } 

if understand correctly, want similar functionality password managers, have functionality of locking after period of time.

first, lets make clear cannot send app background on ios. user.

what can lock application after period of time , display user , password prompt screen. need timer (nstimer), gets restarted @ every action user. if @ time timer gets it's end - 30 second interval passes, timer execute method, can display modal view controller user , password prompt. way app stay locked until user enters username , password.

detecting last action can done in multiple ways:

  • detecting last user's touch
  • adding few lines of code app actions
  • swizzling navigation methods
  • ...

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -