Tag Archives: iCloud

iCloud in your iOS Apps – Part 4: iCloud and Core Data

In this part we’ll talk about how to make your Core Data powered app work with iCloud. This involves adding an option to your persistent store coordinator which will allow Core Data to write log files to iCloud. When changes are detected, the local Core Data store can merge them using those log files.

Here are the iCloud methods I added to the MasterViewController. First the observers under viewDidLoad:

And here are the event handler methods:

Demo Project

Here’s the demo project that I’m building. Don’t forget to change the Bundle ID and your Team ID to make it work on your devices:

Enjoy creating some rad iCloud apps 😉

Further Reading

Watch the whole series

Enjoy!

Watch the full course in one convenient playlist:
Catch this episode on my iOS Dev Diary Podcast:

iCloud in your iOS Apps – Part 3: Document Storage

In this part I’ll show you how to use your app’s ubiquitous folder to store arbitrary files in a user’s iCloud store. We’re not going to cover the UIDocument class here because it adds a layer of complexity to an already difficult subject, and for simple files it’s just not necessary. We’ll also cover how to download new files if iCloud changes have been detected.

There are two code snippets I’m referencing here: one is the method to retrieve your iCloud Documents folder (don’t forget to make it public like I did). This one goes into the AppDelegate.m:

I’m also talking about the NSMetadataQuery which will detect changes in your iCloud Documents folder. This is a custom initialiser so you’ll need to create a property for it. Here’s the initialiser – in the video I’m using it in my ViewController class:

To kick this into action you’ll also need to add an observer under viewDidLoad:

Watch the whole series

Enjoy!

Watch the full course in one convenient playlist:
Catch this episode on my iOS Dev Diary Podcast:

iCloud in your iOS Apps – Part 2: Key/Value Storage

In this part I’ll show you how to store and retrieve data using the NSUbiquitousKeyValueStore singleton, and how to receive the relevant notification so that your app can react if data has changed in iCloud.

Watch the whole series

Enjoy!

Watch the full course in one convenient playlist:
Catch this episode on my iOS Dev Diary Podcast:

iCloud in your iOS Apps – Part 1: Setup

In this 5-part series I’ll show you how to use iCloud in your iOS Apps. We’ll discuss the whole picture, starting with how to setup Xcode and your app, including App ID and Provisioning Profiles and I’ll demonstrate how to use all three flavours of iCloud:

  • Key Value Storage
  • Document Storage
  • and iCloud with Core Data

Setup and Prep Work

In this part I’ll explain how to create an App ID, a Developer Provisioning Profile and how to import it into Xcode. We’ll also discuss what else needs to be setup in Xcode to make your app work with iCloud, no matter which flavour you want to use.

Watch the whole series

Enjoy!

Watch the full course in one convenient playlist:
Catch this episode on my iOS Dev Diary Podcast:

How to use Core Data with iCloud

iCloud-IconUsing Core Data with iCloud seems to be one of the best kept secrets Apple have to offer. For something so relatively simple it’s a surprising brain teaser if you try to follow the scraps of documentation you find.

Let’s take a “human look” at how this works. First we’ll examine the principle, followed by code samples that actually work. It’s less complicated than it seems, so hang in there.

This approach is working fine in iOS 7.1 and 6.1. However it no longer works since iOS 8.

iCloud Core Data in Principle

[emember_protected]

Core Data and iCloud work best with the SQLIte because it’s not an atomically written store (i.e. it does’t have to be saved all at once, but can be saved in chunks). SQLite – much like MySQL – can be updated record by record using log files.

The idea is that you have a local store file, exactly like the one setup by default in the Master/Detail template. You then pass a parameter that will save the log files of each transaction to the iCloud folder. From here all devices read transactions that have not been processed and rebuild the local store files record by record.

Despite what the iOS 6 and earlier documentation may have said, since iOS 7 this is the official Apple recommended approach. In previous years you were encouraged to save the entire store file to the iCloud folder (with a .nosync suffix) but that’s no longer necessary.

To recap: use an SQLite store type and setup your app as you normally would for a “non-iCloud Core Data” project. Have your Team ID ready and an App ID that’s setup to use iCloud.

iCloud Core Data: The Code

In this example I’m starting from the Master Detail template as provided by Xcode 5.1. I’m calling it CDi – short for Core Data iCloud and perhaps an homage to Philips interactive CD project from the early nineties 😉

Continue reading

What is the path to your iCloud Documents folder

iCloud-IconIf you want to know how to access iCloud Documents folder (or ubiquitous documents folder) you need to know your Team ID.

You’ll also need the Bundle ID which can be extracted from the main bundle.

Here’s a method that will deliver you the path to a file. All you need to do is provide the name of a file and it will return the full path to this file in iCloud:

Useful for Core Data Store Files, UIDocuments or plain images.

Note than when documents have populated to iOS devices they need to be downloaded explicitly – otherwise you won’t get a useable file (unlike on OS X which is known as a “greedy peer” and will download the entire file, not just the wrapper).

For completion, here’s how you do just that: