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:
- Take out storesWill and storesDidChange notifications from the applicationDidFinishLaunchingWithOptions method of the AppDelegate and place them into a separate method
- Have a notification listener (custom notification) in the applicationDidFinishLaunchingWithOptions
- This custom notification would trigger those methods when called
- In the SettingsVC, call the custom notification when iCloud has been enabled in the Switch
- Let it perform the required actions in the AppDelegate
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.