Access local settings and settings shared via iCloud with properties.
Add this to your Podfile:
pod 'MYSSharedSettings'
- Subclass
MYSSharedSettings
. - Define properties in
.h
file. - Add
@dynamic
statement for each property in.m
file. - Set
[YourSubclass sharedSettings].syncSettingsWithiCloud = YES; // (or NO)
somewhere in your code.
// CVSharedSettings.h
@interface CVSharedSettings : MYSSharedSettings
@property (nonatomic, assign) BOOL remindersEnabled;
@end
// CVSharedSettings.m
@implementation CVSharedSettings
@dynamic remindersEnabled;
@end
Then you read and set the settings properties like normal:
#import "CVSharedSettings.h"
[YourSubclass sharedSettings].syncSettingsWithiCloud = YES;
[CVSharedSettings sharedSettings].remindersEnabled = YES;
if ([CVSharedSettings sharedSettings].remindersEnabled) {
// ... whatever happens when reminders are enabled
}
The same properties in your app running on other devices will now have the values you've set.
You can override the defaults
method to provide defaults for NSUserDefaults
:
- (NSDictionary *)defaults
{
return @{
@"remindersEnabled" : @YES,
@"timezoneSupportEnabled" : @NO,
@"showDurationOnReadOnlyEvents" : @NO,
@"localRootTableViewMode" : @(CVRootTableViewModeAgenda),
@"hiddenEventCalendarIdentifiers" : @[],
@"customCalendarColors" : @{},
@"defaultEventAlarms" : @[@(MTDateConstantSecondsInMinute * 15)],
@"defaultAllDayEventAlarms" : @[@(MTDateConstantSecondsInHour * 6)],
@"defaultEventReminder" : @[@(MTDateConstantSecondsInMinute * 15)],
@"defaultAllDayReminderAlarms" : @[@(MTDateConstantSecondsInHour * 6)]
};
}
Listen for MYSSharedSettingsChangedNotification
to be informed when remotely changed settings arrive.
Override - (NSString *)keyForPropertyName:(NSString *)propertyName
to transform the property name into the string that will
be used as the key when storing the value in NSUserDefaults
and NSUbiquitousKeyValueStore
.
If you have iCloud syncing enabled but don't want a certain property to be synced, prefix it with local
and MYSSharedSettings will only
store it in NSUSerDefaults. (e.g. localViewMode
)
Call - (void)pushLocalToiCloud
to overwrite any settings currently in iCloud with the local settings in NSUserDefaults
.
ChangedMYSSharedSettingsChangedNotification
to fire both when ubiquity values change, but also local values. And the object is not an array of changed property keys.