I have a fully built Core Data based application in the App Store and with an update I'm working on, I'm bringing iCloud synching capabilities to the app. This is going to be an option for the user to enable instead of having it enabled straight away.
I have a UITabBarController
and in the 5th tab, I have a settings pane where one of the options will be to turn on or off iCloud synching, through the use of a Switch.
What I'm not sure about is how exactly to enable something like this, because the SettingsViewController
(5th Tab) needs to essentially talk to the AppDelegate
to invoke this.
My persistentStoreCoordinator
in the AppDelegate
is below:
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Lite.sqlite"];
NSError *error = nil;
NSURL *cloudURL = [self grabCloudPath:@"iCloud"];
NSDictionary *options = nil;
if ([[NSUserDefaults standardUserDefaults]boolForKey:@"OnLatestVersion"]) {
options = @{ NSMigratePersistentStoresAutomaticallyOption : @YES,
NSInferMappingModelAutomaticallyOption : @YES,
NSPersistentStoreUbiquitousContentURLKey: cloudURL, NSPersistentStoreUbiquitousContentNameKey: @"LiteCloud"};
NSLog(@"Using iCloud from migration");
}
else
{
NSLog(@"Using the other store without the iCloud Migration");
options = @{ NSMigratePersistentStoresAutomaticallyOption : @YES,
NSInferMappingModelAutomaticallyOption : @YES};
}
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _persistentStoreCoordinator;
}
It's not related to this question why the persistentStore is like that, but essentially, I want to ensure that in the SettingsViewController, when the user turns the iCloud switch ON, it invokes the iCloud synching.
I've seen lots of posts relating to the ability to turn off iCloud, but never one that's talked about how to do it, etc.
Edit: Small Update
Through a bit of thinking about this, perhaps I can do this:
Is this correct? I understand it theoretically, if this is the case, but just not sure how best to implement this.
If that is all correct, would something like the below be suitable for the applicationDidFinishLaunchingWithOptions
:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reloadFetchedResults:)
name:@"SomethingChanged"
object:[[UIApplication sharedApplication] delegate]];
How would one go about achieving something like this? Any thoughts would be appreciated.
Setting an NSUserDefaults key, and then using NSNotification to check for that key should probably work. In the settings view, when the switch is flipped, you change a BOOL value in NSUserDefaults, then you call an NSNotification in the appDelegate to update based on the changed NSUserDefaults value. In applicationDidFinishLaunchingWithOptions like you said you can do:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reloadFetchedResults)
name:@"SomethingChanged"
object:nil];
and in reloadFetchedResults you can check for a key in the NSUserDefaults you set in the settings view. In the settings view right after you change the key in NSUserDefaults call [[NSNotificationCenter defaultCenter] postNotification: @"SomethingChanged"];