Last active
January 10, 2025 11:03
-
-
Save pilotmoon/7b39ec42640e48f6b11025132a44a9d1 to your computer and use it in GitHub Desktop.
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
// | |
// NMDefaults.h | |
// nmlib | |
// | |
// Created by Nicholas Moore on 21/12/2023. | |
// | |
#import <Foundation/Foundation.h> | |
NS_ASSUME_NONNULL_BEGIN | |
@interface NMDefaults : NSObject | |
+ (NSUserDefaults *)defaults; | |
+ (NSUserDefaultsController *)defaultsController; | |
@end | |
NS_ASSUME_NONNULL_END |
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
// | |
// NMDefaults.m | |
// nmlib | |
// | |
// Created by Nicholas Moore on 21/12/2023. | |
// | |
#import "NMDefaults.h" | |
static NSUserDefaults *_defaults=nil; | |
static NSUserDefaultsController *_defaultsController=nil; | |
@implementation NMDefaults | |
+ (void)initialize | |
{ | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
// suite name is typically the "parent" (non-setapp) bundle id e.g. com.pilotmoon.popclip | |
NSString *suiteName=[[NSBundle mainBundle] objectForInfoDictionaryKey:@"NMDefaultsSuiteName"]; | |
NSString *bundleId=[[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString *)kCFBundleIdentifierKey]; | |
if (![suiteName isKindOfClass:[NSString class]]||[suiteName isEqualToString:bundleId]) { | |
_defaults=[[NSUserDefaults alloc] initWithSuiteName:nil]; | |
} | |
else { | |
_defaults=[[NSUserDefaults alloc] initWithSuiteName:suiteName]; | |
} | |
_defaultsController=[[NSUserDefaultsController alloc] initWithDefaults:_defaults initialValues:nil]; | |
}); | |
} | |
+ (NSUserDefaults *)defaults { | |
return _defaults; | |
} | |
+ (NSUserDefaultsController *)defaultsController { | |
return _defaultsController; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment