Last active
May 14, 2020 08:17
-
-
Save phatblat/f640416c15e11b685511 to your computer and use it in GitHub Desktop.
Repost NSUserDefaultsDidChangeNotification to iOS extensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma mark - App Singleton | |
- (instancetype)init { | |
... | |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userDefaultsDidChange:) name:NSUserDefaultsDidChangeNotification object:nil]; | |
... | |
} | |
- (void)dealloc { | |
[[NSNotificationCenter defaultCenter] removeObserver:self]; | |
} | |
- (void)userDefaultsDidChange:(NSNotification *)notification { | |
CFNotificationCenterRef const center = CFNotificationCenterGetDarwinNotifyCenter(); | |
CFNotificationCenterPostNotification(center, (__bridge CFStringRef)@"defaultsChangedOnTheOtherSide", NULL, NULL, YES); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma mark - Extension Observers | |
- (instancetype)init { | |
... | |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userDefaultsDidChange:) name:@"localDefaultsChangedNotification" object:nil]; | |
... | |
} | |
- (void)dealloc { | |
[[NSNotificationCenter defaultCenter] removeObserver:self]; | |
} | |
- (void)userDefaultsDidChange:(NSNotification *)notification { | |
// Do stuff | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma mark - Extension Singleton | |
- (instancetype)init { | |
... | |
[self registerForDarwinNotifications]; | |
... | |
} | |
- (void)dealloc { | |
[self unregisterForDarwinNotifications]; | |
} | |
- (void)registerForDarwinNotifications { | |
CFNotificationCenterRef const center = CFNotificationCenterGetDarwinNotifyCenter(); | |
CFStringRef str = (__bridge CFStringRef)@"defaultsChangedOnTheOtherSide"; | |
CFNotificationCenterAddObserver(center, | |
(__bridge const void *)(self), | |
notificationCallback, | |
str, | |
NULL, | |
CFNotificationSuspensionBehaviorDeliverImmediately); | |
} | |
- (void)unregisterForDarwinNotifications { | |
CFNotificationCenterRef const center = CFNotificationCenterGetDarwinNotifyCenter(); | |
CFStringRef str = (__bridge CFStringRef)@"defaultsChangedOnTheOtherSide"; | |
CFNotificationCenterRemoveObserver(center, | |
(__bridge const void *)(self), | |
str, | |
NULL); | |
} | |
void notificationCallback(CFNotificationCenterRef center, | |
void * observer, | |
CFStringRef name, | |
void const * object, | |
CFDictionaryRef userInfo) { | |
[[NSNotificationCenter defaultCenter] postNotificationName:@"localDefaultsChangedNotification" | |
object:nil | |
userInfo:nil]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment