For the More Curious: Application State Transitions

Let’s write some quick code to get a better understanding of the different application state transitions.

You already know about self, an implicit variable that points to the instance that is executing the current method. There is another implicit variable called _cmd, which is the selector for the current method. You can get the NSString representation of a selector with the function NSStringFromSelector.

In BNRAppDelegate.m, implement the application state transition delegate methods so that they print out the name of the method. You will need to add four more methods. (Check to make sure the template has not already created these methods before writing brand new ones.)

-​ ​(​v​o​i​d​)​a​p​p​l​i​c​a​t​i​o​n​W​i​l​l​R​e​s​i​g​n​A​c​t​i​v​e​:​(​U​I​A​p​p​l​i​c​a​t​i​o​n​ ​*​)​a​p​p​l​i​c​a​t​i​o​n​
{​
 ​ ​ ​ ​N​S​L​o​g​(​@​"​%​@​"​,​ ​N​S​S​t​r​i​n​g​F​r​o​m​S​e​l​e​c​t​o​r​(​_​c​m​d​)​)​;​
}​

-​ ​(​v​o​i​d​)​a​p​p​l​i​c​a​t​i​o​n​W​i​l​l​E​n​t​e​r​F​o​r​e​g​r​o​u​n​d​:​(​U​I​A​p​p​l​i​c​a​t​i​o​n​ ​*​)​a​p​p​l​i​c​a​t​i​o​n​
{​
 ​ ​ ​ ​N​S​L​o​g​(​@​"​%​@​"​,​ ​N​S​S​t​r​i​n​g​F​r​o​m​S​e​l​e​c​t​o​r​(​_​c​m​d​)​)​;​
}​

-​ ​(​v​o​i​d​)​a​p​p​l​i​c​a​t​i​o​n​D​i​d​B​e​c​o​m​e​A​c​t​i​v​e​:​(​U​I​A​p​p​l​i​c​a​t​i​o​n​ ​*​)​a​p​p​l​i​c​a​t​i​o​n​
{​
 ​ ​ ​ ​N​S​L​o​g​(​@​"​%​@​"​,​ ​N​S​S​t​r​i​n​g​F​r​o​m​S​e​l​e​c​t​o​r​(​_​c​m​d​)​)​;​
}​

-​ ​(​v​o​i​d​)​a​p​p​l​i​c​a​t​i​o​n​W​i​l​l​T​e​r​m​i​n​a​t​e​:​(​U​I​A​p​p​l​i​c​a​t​i​o​n​ ​*​)​a​p​p​l​i​c​a​t​i​o​n​
{​
 ​ ​ ​ ​N​S​L​o​g​(​@​"​%​@​"​,​ ​N​S​S​t​r​i​n​g​F​r​o​m​S​e​l​e​c​t​o​r​(​_​c​m​d​)​)​;​
}​


Now, add the following NSLog statements to the top of application:didFinishLaunchingWithOptions: and applicationDidEnterBackground:.

-​ ​(​B​O​O​L​)​a​p​p​l​i​c​a​t​i​o​n​:​(​U​I​A​p​p​l​i​c​a​t​i​o​n​ ​*​)​a​p​p​l​i​c​a​t​i​o​n​
 ​ ​ ​d​i​d​F​i​n​i​s​h​L​a​u​n​c​h​i​n​g​W​i​t​h​O​p​t​i​o​n​s​:​(​N​S​D​i​c​t​i​o​n​a​r​y​ ​*​)​l​a​u​n​c​h​O​p​t​i​o​n​s​
{​
 ​ ​ ​ ​N​S​L​o​g​(​@​"​%​@​"​,​ ​N​S​S​t​r​i​n​g​F​r​o​m​S​e​l​e​c​t​o​r​(​_​c​m​d​)​)​;​
 ​ ​ ​ ​.​.​.​
}​

-​ ​(​v​o​i​d​)​a​p​p​l​i​c​a​t​i​o​n​D​i​d​E​n​t​e​r​B​a​c​k​g​r​o​u​n​d​:​(​U​I​A​p​p​l​i​c​a​t​i​o​n​ ​*​)​a​p​p​l​i​c​a​t​i​o​n​
{​
 ​ ​ ​ ​N​S​L​o​g​(​@​"​%​@​"​,​ ​N​S​S​t​r​i​n​g​F​r​o​m​S​e​l​e​c​t​o​r​(​_​c​m​d​)​)​;​
 ​ ​ ​ ​[​[​B​N​R​I​t​e​m​S​t​o​r​e​ ​s​h​a​r​e​d​S​t​o​r​e​]​ ​s​a​v​e​C​h​a​n​g​e​s​]​;​
}​

Build and run the application. You will see that the application gets sent application:didFinishLaunchingWithOptions: and then applicationDidBecomeActive:. Play around some to see what actions cause what transitions.

Press the Home button, and the console will report that the application briefly inactivated and then went to the background state. Relaunch the application by tapping its icon on the Home screen or in the multitasking display. The console will report that the application entered the foreground and then became active.

Press the Home button to exit the application again. Then, double-click the Home button to open the multitasking display. Swipe the Homepwner application up and off this display to quit the application. Note that no message is sent to your application delegate at this point – it is simply terminated.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset