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:
#import <Foundation/Foundation.h> #import "Logger.h" int main (int argc, const char * argv[]) { @autoreleasepool { Logger *logger = [[Logger alloc] init]; [[NSNotificationCenter defaultCenter] addObserver:logger selector:@selector(zoneChange:) name:NSSystemTimeZoneDidChangeNotification object:nil]; NSURL *url = [NSURL URLWithString: @"http://www.gutenberg.org/cache/epub/205/pg205.txt"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; __unused NSURLConnection *fetchConn = [[NSURLConnection alloc] initWithRequest:request delegate:logger startImmediately:YES]; __unused NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:logger selector:@selector(sayOuch:) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] run]; } return 0; }
Now implement the method that will get called in Logger.m:
- (void)zoneChange:(NSNotification *)note { NSLog(@"The system time zone has changed!"); }
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.)