I am trying to use iCloud with my iOS app. I have an application running on separate devices, but when I added iCloud, nothing happened. I looked all over the internet and found only one example that I did not understand. My application is iOS 5.0 and uses basic data to store pages. Below is my application delegate and my presentation in which I show it. Sorry for the lack of knowledge when it comes to iPhone dev. Please help. http://goddess-gate.com/dc2/index.php/post/452
Thank you
If someone knows / has a full working iCloud + CoreData working draft, I believe that I can understand this. Right now, I just have code that I don’t even know how they are called ... If I have a complete project, I can go through it so that I can fully understand how it works.
The problem is that I don’t think that something is being called to update the view with data, and I don’t believe that it sends it to the cloud, but I don’t know what to call ...
PageFlipperAppDelegate.h
PageFlipperAppDelegate.m
#import "PageFlipperAppDelegate.h" @implementation PageFlipperAppDelegate @synthesize window; @synthesize managedObjectContext, managedObjectModel, persistentStoreCoordinator,collectionViewController; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque]; [NSThread sleepForTimeInterval:2.75]; collectionViewController = [[PagesCollectionViewController alloc] initWithManagedObjectContext:[self managedObjectContext]]; [(UINavigationController *)[[self window] rootViewController] pushViewController:collectionViewController animated:NO]; [collectionViewController release]; [[self window] makeKeyAndVisible]; return YES; } - (void)applicationWillResignActive:(UIApplication *)application { //stopping timer since we're going to background // [(UiWindowSubclass *)self.window stopTimer]; } - (void)applicationDidBecomeActive:(UIApplication *)application { } - (void)applicationDidEnterBackground:(UIApplication *)application { [self saveContext]; } - (void)applicationWillEnterForeground:(UIApplication *)application { } - (void)applicationWillTerminate:(UIApplication *)application { [self saveContext]; } - (void)dealloc { [window release]; [managedObjectContext release]; [managedObjectModel release]; [persistentStoreCoordinator release]; [super dealloc]; } - (void)awakeFromNib { } - (void)saveContext { NSError *error = nil; if ([self managedObjectContext]) { if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) { NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } } } #pragma mark - Core Data stack - (NSManagedObjectContext *)managedObjectContext { if (managedObjectContext != nil) { return managedObjectContext; } NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; if (coordinator != nil) { //if (IOS_VERSION_GREATER_THAN_OR_EQUAL_TO(@"5.0")) { NSManagedObjectContext* moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType]; [moc performBlockAndWait:^{ [moc setPersistentStoreCoordinator: coordinator]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(mergeChangesFrom_iCloud:) name:NSPersistentStoreDidImportUbiquitousContentChangesNotification object:coordinator]; }]; managedObjectContext = moc; } return managedObjectContext; } /** Returns the managed object model for the application. If the model doesn't already exist, it is created from the application model. */ - (NSManagedObjectModel *)managedObjectModel { if (!managedObjectModel) { NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"PageFlipper" withExtension:@"momd"]; managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; } return managedObjectModel; } /** Returns the persistent store coordinator for the application. If the coordinator doesn't already exist, it is created and the application store added to it. - (NSPersistentStoreCoordinator *)persistentStoreCoordinator { if (persistentStoreCoordinator != nil) { return persistentStoreCoordinator; } NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"PageFlipper.sqlite"]; persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; NSPersistentStoreCoordinator* psc = persistentStoreCoordinator; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSFileManager *fileManager = [NSFileManager defaultManager]; // Migrate datamodel NSDictionary *options = nil; // this needs to match the entitlements and provisioning profile NSURL *cloudURL = [fileManager URLForUbiquityContainerIdentifier:@"G88FQ4WK29.com.brandonsdesigngroup.3Doodles"]; NSString* coreDataCloudContent = [[cloudURL path] stringByAppendingPathComponent:@"data"]; if ([coreDataCloudContent length] != 0) { // iCloud is available cloudURL = [NSURL fileURLWithPath:coreDataCloudContent]; options = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, @"3Doodles.store", NSPersistentStoreUbiquitousContentNameKey, cloudURL, NSPersistentStoreUbiquitousContentURLKey, nil]; } else { // iCloud is not available options = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; } NSError *error = nil; [psc lock]; if (![psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) { NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } [psc unlock]; dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"asynchronously added persistent store!"); [[NSNotificationCenter defaultCenter] postNotificationName:@"RefetchAllDatabaseData" object:self userInfo:nil]; }); }); return persistentStoreCoordinator; } - (void)mergeiCloudChanges:(NSNotification*)note forContext:(NSManagedObjectContext*)moc { [moc mergeChangesFromContextDidSaveNotification:note]; NSNotification* refreshNotification = [NSNotification notificationWithName:@"RefreshAllViews" object:self userInfo:[note userInfo]]; [[NSNotificationCenter defaultCenter] postNotification:refreshNotification]; } - (void)mergeChangesFrom_iCloud:(NSNotification *)notification { NSManagedObjectContext* moc = [self managedObjectContext]; // this only works if you used NSMainQueueConcurrencyType // otherwise use a dispatch_async back to the main thread yourself [moc performBlock:^{ [self mergeiCloudChanges:notification forContext:moc]; }]; } #pragma mark - Application Documents directory /** Returns the URL to the application Documents directory. */ - (NSURL *)applicationDocumentsDirectory { return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; } @end
viewController.h
viewController.m