iCloud Sync tutorial Part 1/2 - create iCloudStore, check if user is Signed in, migrate store

In this IOS 9, Xcode 7 iCloud Sync tutorial, we create an iCloud store and checked if the user is signed in. If yes, we migrate local Store to iCloud.


  1. Do the Core Data tutorial Part 1-5, or use a CoreData IOS App to test this iCloud sync tutorial!
  2. Set iCloud "ON" at the App Target
  3. Check U ser Account iCloud Logged in
  4. Set Stores
  5. Migration Process
  6. Load iCloudStore - Listen to Changes
  7. Update your calls:
  8. Test iCloud store
  9. Check, if the user is Signed in iCloud

Parts of the series:

If you open an old Xcode Project and change the Target to IOS 9, you will get some deprecated warnings. You need to solve them. These Articles can help you:

  1. OS X El Capitan & Xcode 7 Upgrade error Solution - IOS 9 SDK Frameworks directory
  2. IOS9 SDK Xcode 7 deprecated warnings solutions

Core Data tutorials:

  1. “You must to know!” Collection - Core Data simple explanations
  2. Core Data tutorial Part 1 / 5 - Create Managed Object Model with Attributes, Relationships & Delete Rules
  3. Core Data tutorial Part 2 / 5 - Save Entity’s Attributes, Relationships, and make FetchRequest
  4. Core Data tutorial Part 3 / 5 - Edit, Delete NSManagedObjects
  5. Core Data part 4 / 5 - Change Core Data model, using SortDescriptor, NSPredicate FetchRequest and filter Array using Predicate
  6. Core Data tutorial part 4.2 / 5 - Using NSFetchedResultsController, Reorder TableViewCells with LongPressGesture
  7. CoreData Tutorial Part 5/5 - Model Versioning, Lightweight Migration, Model Mapping


1. Set iCloud "ON" at the App Target

1.1. Check at the Target the Team, select your valid developer Profile

SpotlessiCode | iCloud Sync tutorial | set iCloud 1_1_1

If you have a Team with valid developer Profile, just select it. If you need to create a new one, select "Add an Account", and do every step which needs to set the profile and App ID, with certificates and provisioning profiles. 

SpotlessiCode | iCloud Sync tutorial | set iCloud 1_1_2

1.2. If you have "Code signing identity error" - click on "Fix issue"

If you have a valid profile, but comes this issue, easily click on fix issues button.

SpotlessiCode | iCloud Sync tutorial | set iCloud 1_1_3

SpotlessiCode | iCloud Sync tutorial | set iCloud 1_1_4

1.3.  At Capabilities switch iCloud from "off" to "on"

SpotlessiCode | iCloud Sync tutorial | set iCloud 1_1_5

SpotlessiCode | iCloud Sync tutorial | set iCloud 1_1_6

Select the "Key-value storage and iCloud Documents, use default container.

SpotlessiCode | iCloud Sync tutorial | set iCloud 1_1_7

If it needs, "Fix Issues"

SpotlessiCode | iCloud Sync tutorial | set iCloud 1_1_8

2. Check user Account iCloud Logged in

2.1. Add the check code in your AppDelegate.m before @end:

2.2. And in your AppDelegate.h file add this line:

- (BOOL)iCloudAccountIsSignedIn;

SpotlessiCode | iCloud Sync tutorial | set iCloud 2_2

2.3. Put this code in your AppDelegate.m file, at didFinishLaunchingWithOptions:

[self iCloudAccountIsSignedIn];

SpotlessiCode | iCloud Sync tutorial | check account signed in 2_3

2.4. Run you App in Simulator, and check your logs

Your result, fi you are not logged in:

SpotlessiCode | iCloud Sync tutorial | set iCloud 2_4

2.5. Change iCloud settings in Simulator

Logged in with your iCloud account or create a new one, after that check, that iCloud Drive is on:

SpotlessiCode | iCloud Sync tutorial | Simulator sign in 2_5_1

SpotlessiCode | iCloud Sync tutorial | iCloud signed in drive on 2_5_2

2.6. Run your App again, and check your logs again - it will say that you are logged in

SpotlessiCode | iCloud Sync tutorial | set iCloud logged in 2_6

3. Set Stores

3.1. Add two persistentStore property in your AppDelegate.h file:

SpotlessiCode | iCloud Sync tutorial | set iCloud store 3_1

3.2. And Add two filename string in your AppDelegate.m file:

SpotlessiCode | iCloud Sync tutorial | set iCloud filename 3_2

3.3. Change applicationStoreDirectory and applicationDocumentDirectory codes to this:

3.4. Add storeURL and iCloudStoreURL NSURL codes in AppDelegate.m after Directory codes:

3.5. Create -(id)init SETUP code after NSURL code:


and DELETE these codes from AppDelegate.m: 

  • - (NSManagedObjectModel *)managedObjectModel
  • - (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
  • - (NSManagedObjectContext *)managedObjectContext

3.6. Create -(void)setupCoreData and -(void)loadStore method:

Put -(void)setupCoreData; into your AppDelagate.h file. After that Add these codes to your AppDelegate.m file after -(id)init code:

4. Migration Process

You need to check if the database needs migration, and if it needs, you must to do it in the background, so the user can continue to use the UI without froze. MigrationManager is important to migrate files, data form one store to another. So you need to check first, which store exists, and it necessary to change or not. For the whole Migration Process, you need these codes. Please check the comments in the code to understand exactly what is happening. You can learn more here: Migration Process.

Put these codes after -(void)loadStore process:

5. Load iCloudStore - Listen to Changes

Now we can load the iCloudStore, and check changes in the Log.

Put this code after your  - (BOOL)iCloudAccountIsSignedIn. Check the comments to understand which code what handle.

6. Update your calls:

After your "applicationDidBecameActive" called, need to start your "setupCoreData" block, and with "init", need to call "listenForStoreChanges" too. So now we set these:

Put in your AppDelegate.m file this: - (BOOL)reloadStore;

Put this in your -(id)init code before return self;[self listenForStoreChanges];

Change your -(void)setupCoreData code to this:

Put this: [self setupCoreData]; into your - (void)applicationDidBecomeActive:(UIApplication *)application; method.

7. Test iCloud Store

Run your Application, and check the logs. If you did everything nice, now you can see first "Using local storage 1", later "Using local storage 0". The first means that your app is now using a temporary local persistent store. The second, the 0 means, that that your app is currently using the iCloud-enabled persistent store. You can learn more about iCloud debugging here: Using the iCloud Debugging Tools

SpotlessiCode | iCloud Sync tutorial | test_icloud sync 7_1

SpotlessiCode | iCloud Sync tutorial | debugging iCloud 7_2

8. Check, if the user is Signed in iCloud

If the user is Signed in, we will load iCloudStore, but if not, we can load just the local store. Change your setupCoreData to this:

Run your Simulator again, and check the result. If you are Signed in into iCloud on your Simulator Device, you will get "Using local storage:0". 

Now Log out on your simulator device, and run the App again.

SpotlessiCode | iCloud Sync tutorial | sign out 8_1

SpotlessiCode | iCloud Sync tutorial | clear iCloud account 8_2

You have this line in your logs because your App loads the local store only: "Load the Non-iCloud store"

SpotlessiCode | iCloud Sync tutorial | load the non iCloud store 8_3


Download the Xcode template from Github.


The next part of the series: iCloud Part 2/2 - soon!

Share & Follow

If you would like to be notified of my new solutions, please subscribe to my Newsletter or my youtube channel and/ or follow me on Facebook or Twitter! Check Footer section for these opportunities!