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.
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:
Core Data tutorials:
1.1. Check at the Target the Team, select your valid developer Profile
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.
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.
1.3. At Capabilities switch iCloud from "off" to "on"
Select the "Key-value storage and iCloud Documents, use default container.
If it needs, "Fix Issues"
2.1. Add the check code in your AppDelegate.m before @end:
2.2. And in your AppDelegate.h file add this line:
2.3. Put this code in your AppDelegate.m file, at didFinishLaunchingWithOptions:
2.4. Run you App in Simulator, and check your logs
Your result, fi you are not logged in:
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:
2.6. Run your App again, and check your logs again - it will say that you are logged in
3.1. Add two persistentStore property in your AppDelegate.h file:
3.2. And Add two filename string in your AppDelegate.m file:
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:
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:
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:
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.
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.
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
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.
You have this line in your logs because your App loads the local store only: "Load the Non-iCloud store"
Download the Xcode template from Github.
The next part of the series: iCloud Part 2/2 - soon!
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!