当用户修改MAC系统的时间时,通知程序。
在.m文件中加入方法:
#import "BNRLogger.h"
@implementation BNRLogger
- (NSString *)lastTimerString{
static NSDateFormatter *dateFormatter = nil;
if(!dateFormatter) {
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSLog(@"created dateFormatter");
}
return [dateFormatter stringFromDate:self.lastTime];
}
- (void)updateLastTimer:(NSTimer *)t {
NSDate *now = [NSDate date];
[self setLastTime:now];
NSLog(@"I want to see the timer here: ");
NSLog(@"Just set time to %@", self.lastTimerString);
}
- (void)zoneChange:(NSNotification *)note{
NSLog(@"The system time zone has changed!");
}
@end
在main函数中注册并调用:
#import <Foundation/Foundation.h>
#import "BNRLogger.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
BNRLogger *logger = [[BNRLogger alloc] init];
[[NSNotificationCenter defaultCenter]
addObserver:logger
selector:@selector(zoneChange:)
name:NSSystemTimeZoneDidChangeNotification
object:nil];
__unused NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0
target:logger
selector:@selector(updateLastTimer:)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] run];
}
return 0;
}
查看log:
2018-03-14 22:31:01.156010+0800 TOCRunloopa[78189:4808386] I want to see the timer here:
2018-03-14 22:31:01.156324+0800 TOCRunloopa[78189:4808386] created dateFormatter
2018-03-14 22:31:01.159161+0800 TOCRunloopa[78189:4808386] Just set time to 2018年3月14日 下午10:31:01
2018-03-14 22:31:03.156022+0800 TOCRunloopa[78189:4808386] I want to see the timer here:
2018-03-14 22:31:03.156175+0800 TOCRunloopa[78189:4808386] Just set time to 2018年3月14日 下午10:31:03
2018-03-14 22:31:05.156034+0800 TOCRunloopa[78189:4808386] I want to see the timer here:
2018-03-14 22:31:05.156163+0800 TOCRunloopa[78189:4808386] Just set time to 2018年3月14日 下午10:31:05
2018-03-14 22:31:07.155929+0800 TOCRunloopa[78189:4808386] I want to see the timer here:
2018-03-14 22:31:07.156086+0800 TOCRunloopa[78189:4808386] Just set time to 2018年3月14日 下午10:31:07
2018-03-14 22:31:09.155590+0800 TOCRunloopa[78189:4808386] I want to see the timer here:
2018-03-14 22:31:09.155950+0800 TOCRunloopa[78189:4808386] Just set time to 2018年3月14日 下午10:31:09
2018-03-14 22:31:11.154851+0800 TOCRunloopa[78189:4808386] I want to see the timer here:
2018-03-14 22:31:11.155819+0800 TOCRunloopa[78189:4808386] Just set time to 2018年3月14日 下午10:31:11
2018-03-14 22:31:13.156983+0800 TOCRunloopa[78189:4808386] I want to see the timer here:
2018-03-14 22:31:13.157159+0800 TOCRunloopa[78189:4808386] Just set time to 2018年3月14日 下午10:31:13
2018-03-14 22:31:15.153715+0800 TOCRunloopa[78189:4808386] I want to see the timer here:
2018-03-14 22:31:15.153921+0800 TOCRunloopa[78189:4808386] Just set time to 2018年3月14日 下午10:31:15
2018-03-14 22:31:17.155811+0800 TOCRunloopa[78189:4808386] I want to see the timer here:
2018-03-14 22:31:17.156208+0800 TOCRunloopa[78189:4808386] Just set time to 2018年3月14日 下午10:31:17
2018-03-14 22:31:19.157613+0800 TOCRunloopa[78189:4808386] I want to see the timer here:
2018-03-14 22:31:19.158116+0800 TOCRunloopa[78189:4808386] Just set time to 2018年3月14日 下午10:31:19
2018-03-14 22:31:20.693787+0800 TOCRunloopa[78189:4808386] The system time zone has changed!
2018-03-14 22:31:21.155836+0800 TOCRunloopa[78189:4808386] I want to see the timer here:
2018-03-14 22:31:21.161952+0800 TOCRunloopa[78189:4808386] Just set time to 2018年3月14日 下午10:31:21
2018-03-14 22:31:23.157004+0800 TOCRunloopa[78189:4808386] I want to see the timer here:
2018-03-14 22:31:23.157178+0800 TOCRunloopa[78189:4808386] Just set time to 2018年3月14日 下午10:31:23