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 HomepwnerAppDelegate.m, implement the application state transition delegate methods so that they print out the name of the method. You’ll need to add three more methods:

-​ ​(​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​)​)​;​
}​

Now, add the following NSLog statements to the top of application:​didFinishLaunchingWithOptions:, applicationWillTerminate:, 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​)​)​;​
 ​ ​ ​ ​[​[​P​o​s​s​e​s​s​i​o​n​S​t​o​r​e​ ​d​e​f​a​u​l​t​S​t​o​r​e​]​ ​s​a​v​e​C​h​a​n​g​e​s​]​;​
}​

-​ ​(​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​)​)​;​
 ​ ​ ​ ​[​[​P​o​s​s​e​s​s​i​o​n​S​t​o​r​e​ ​d​e​f​a​u​l​t​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:. Click 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 dock. The console will report that the application entered the foreground and then became active. Double-click the Home button to launch the dock and then touch and hold the Homepwner icon until it begins to jiggle. Tap the red terminate button in the icon’s upper left corner and note that no message is sent to your application delegate – it is simply terminated immediately.

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

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