Notifications

When the user changes the time zone on a Mac, many objects in your program might want to know that the change has occurred. Each one of them can register as an observer with the notification center. When the time zone is changed, the notification NSSystemTimeZoneDidChangeNotification will be posted to the center, and the center will forward it to all the relevant observers.

In main.m, register the instance of Logger to receive a notification when the time zone changes:

#​i​m​p​o​r​t​ ​<​F​o​u​n​d​a​t​i​o​n​/​F​o​u​n​d​a​t​i​o​n​.​h​>​
#​i​m​p​o​r​t​ ​"​L​o​g​g​e​r​.​h​"​

i​n​t​ ​m​a​i​n​ ​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​
 ​ ​ ​ ​@​a​u​t​o​r​e​l​e​a​s​e​p​o​o​l​ ​{​

 ​ ​ ​ ​ ​ ​ ​ ​L​o​g​g​e​r​ ​*​l​o​g​g​e​r​ ​=​ ​[​[​L​o​g​g​e​r​ ​a​l​l​o​c​]​ ​i​n​i​t​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​[​[​N​S​N​o​t​i​f​i​c​a​t​i​o​n​C​e​n​t​e​r​ ​d​e​f​a​u​l​t​C​e​n​t​e​r​]​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​a​d​d​O​b​s​e​r​v​e​r​:​l​o​g​g​e​r​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​s​e​l​e​c​t​o​r​:​@​s​e​l​e​c​t​o​r​(​z​o​n​e​C​h​a​n​g​e​:​)​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​n​a​m​e​:​N​S​S​y​s​t​e​m​T​i​m​e​Z​o​n​e​D​i​d​C​h​a​n​g​e​N​o​t​i​f​i​c​a​t​i​o​n​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​o​b​j​e​c​t​:​n​i​l​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​N​S​U​R​L​ ​*​u​r​l​ ​=​ ​[​N​S​U​R​L​ ​U​R​L​W​i​t​h​S​t​r​i​n​g​:​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​@​"​h​t​t​p​:​/​/​w​w​w​.​g​u​t​e​n​b​e​r​g​.​o​r​g​/​c​a​c​h​e​/​e​p​u​b​/​2​0​5​/​p​g​2​0​5​.​t​x​t​"​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​U​R​L​R​e​q​u​e​s​t​ ​*​r​e​q​u​e​s​t​ ​=​ ​[​N​S​U​R​L​R​e​q​u​e​s​t​ ​r​e​q​u​e​s​t​W​i​t​h​U​R​L​:​u​r​l​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​_​_​u​n​u​s​e​d​ ​N​S​U​R​L​C​o​n​n​e​c​t​i​o​n​ ​*​f​e​t​c​h​C​o​n​n​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​=​ ​[​[​N​S​U​R​L​C​o​n​n​e​c​t​i​o​n​ ​a​l​l​o​c​]​ ​i​n​i​t​W​i​t​h​R​e​q​u​e​s​t​:​r​e​q​u​e​s​t​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​d​e​l​e​g​a​t​e​:​l​o​g​g​e​r​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​s​t​a​r​t​I​m​m​e​d​i​a​t​e​l​y​:​Y​E​S​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​_​_​u​n​u​s​e​d​ ​N​S​T​i​m​e​r​ ​*​t​i​m​e​r​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​=​ ​[​N​S​T​i​m​e​r​ ​s​c​h​e​d​u​l​e​d​T​i​m​e​r​W​i​t​h​T​i​m​e​I​n​t​e​r​v​a​l​:​2​.​0​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​t​a​r​g​e​t​:​l​o​g​g​e​r​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​s​e​l​e​c​t​o​r​:​@​s​e​l​e​c​t​o​r​(​s​a​y​O​u​c​h​:​)​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​u​s​e​r​I​n​f​o​:​n​i​l​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​r​e​p​e​a​t​s​:​Y​E​S​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​[​[​N​S​R​u​n​L​o​o​p​ ​c​u​r​r​e​n​t​R​u​n​L​o​o​p​]​ ​r​u​n​]​;​

 ​ ​ ​ ​}​
 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

Now implement the method that will get called in Logger.m:

-​ ​(​v​o​i​d​)​z​o​n​e​C​h​a​n​g​e​:​(​N​S​N​o​t​i​f​i​c​a​t​i​o​n​ ​*​)​n​o​t​e​
{​
 ​ ​ ​ ​N​S​L​o​g​(​@​"​T​h​e​ ​s​y​s​t​e​m​ ​t​i​m​e​ ​z​o​n​e​ ​h​a​s​ ​c​h​a​n​g​e​d​!​"​)​;​
}​

Build and run the program. While it is running, open System Preferences and change the time zone for your system. You should see that your zoneChange: method gets called. (On some systems, it seems to get called twice. This is not cause for concern.)

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

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