What to do when the user enables or disables the iCloud support in the app Settings Bundle?

263 views Asked by At

I am targeting iOS7 and I use iCloud with an UIManagedDocument. I would like to know what to do in these two cases:

1) First case: App first launch

It's the first launch, when we do not know yet if the user wants to use iCloud or not and we should assume he doesn't want to use it until we ask him. Once we'll find out, his choice will be stored in the app Settings Bundle and he will be able to change his mind later.

When my app launch, the first view that appears triggers the UIManagedDocument init with [[UIManagedDocument alloc] initWithFileURL:url]. It does this because it should know how many rows it has to display in its table.

At this point, it is the first launch, and I still have to ask to the user, I assume he does not want to use iCloud so the UIManagedDocument is initialized without the persistentStoreOptions set (NSPersistentStoreUbiquitousContentNameKey and NSPersistentStoreUbiquitousContentURLKey).

The file is then created on the disk with

[UIManagedDocument saveToURL: URL 
            forSaveOperation: UIDocumentSaveForCreating 
           completionHandler: ^(BOOL success){

            if(success){
              //Do something with the document
            }

           }];

then the view appears. Since it is the first launch, I should ask the user if he wants to use iCloud or not.

I show an UIAlertView from [viewDidAppear:animated]

If he answers "NO", everything is already ok. But what should I do if he says "YES"??

Should I close the UIManagedDocument and migrate the DB or is it enough to set the NSPersistentStoreUbiquitousContentNameKey and NSPersistentStoreUbiquitousContentURLKey in the document's persistentStoreOptions?

2) Second case: a couple of days later the user changes his mind

The user did choose to enable iCloud but after some day he changes his mind. He switches to NO the iCloud option in the app settings bundle. When the app launches I check for a change in this option and I find that it has switched from YES to NO. As I understood from the hints I got on SO, the correct path should be to display an UIAlertView asking him if he wants:

a) Keep the documents on the device

b) Delete the documents from the device

c) Keep using iCloud

What should i do for the options a) and b)? A DB migration for option a) and recreate the DB from zero for option b)?

2

There are 2 answers

4
Scott Berrevoets On BEST ANSWER

The way you're using the term "migration" is a little confusing, as incrementing the Core Data model from one version to a new one is also called migration. I assume you're just talking about "migrating" from iCloud to local only or vice versa.

In your first case, I wouldn't create the UIManagedDocument until the user has made a choice. If the user wants to use iCloud, all you have to do is take that and open it and you don't have to worry about closing/removing the local document (which is now pointless).

In the second case, if the user chooses option A, copy the file from iCloud to local, then remove it from iCloud. Option B means "remove from iCloud, I don't want it anymore". If you still want to continue using Core Data locally, create a new document for it. C, of course, would do nothing, just dismiss the alert.

0
Duncan Groenewald On

Typically there are two places to check whether iCloud settings have changed and/or to check or open any files: 1) in the AppDelegate method application:didfinishlaunchingwithoptions, and 2) in the AppDelegate method applicationwillenterforeground

In addition if you register for iCloud account change notifications you can respond to them.

This allows you to organise things before your view starts trying to display any core data items. Opening or Migrating a file may take some time so you may have to display some other activity indicator view while this is being performed.

I would not create the document in the view that displays data because the call to open the document is performed asynchronously and until the completion handler runs you don't know when it's going to complete. Rather you should use the completion handler to launch this view because then you know it's safe to start accessing the core data objects.

More details on how to achieve UIManagedDocument & iCloud Integration here.