2

I have a free version of an app and a paid version. I'd like to give users the option to import their existing data from the free version into the paid version.

Currently the data is stored in Core Data.

I've looked at existing solutions across the internet and SO, the existing solutions suggest making a request from the free application to the paid application using a URL request that contains the data. (e.g. http://mobileorchard.com/lite-to-paid-iphone-application-data-migrations-with-custom-url-handlers/)

So my question is, how is best to implement the solution in Swift:

Is the URL method still the best approach? Are there any code samples available?

One idea I've had is to convert the entire DB to JSON, then to make a request with the JSON payload and deserialise it into Core Data the other side. Create json string from core data and vice versa?

Community
  • 1
  • 1
Phil Hudson
  • 3,819
  • 8
  • 35
  • 59

2 Answers2

2

What I'd do is set up an app group that both apps can access. Put your data in the app group folder and access it from both versions. You don't need to copy it, just leave it where it is.

To do this:

  1. Set up an app group in the "App Groups" section of the target settings in Xcode. Use the same app group for both versions.
  2. Find the location of the app group folder with:

    NSURL *groupURL = [[NSFileManager defaultManager]
        containerURLForSecurityApplicationGroupIdentifier:
            @"GROUP_NAME_HERE"];
    

    Or in Swift:

    //swift
    let groupURL = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("groupIdentifier")
    
  3. Modify your Core Data setup code to put the persistent store file in the directory you found in the previous step.

Since you have existing apps, you probably want to move the existing data into the new app group directory. You'd do this by using migratePersistentStore:toURL:options:withType:error: to move the existing store to the new location from step 2.

At this point both apps can read and write the same directory. When people buy the paid app, the data is literally already there for it.

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170
  • Dude this is awesome, so clean, so easy - an elegant solution. I'll add the swift equivalent too! – Phil Hudson Apr 15 '16 at 01:19
  • @PhilHudson Thanks for the edit suggestion, I don't know why people rejected it. – Tom Harrington Apr 15 '16 at 02:25
  • App group is working really well (for the app that hasn't launched yet). I'm looking into migrating the data (and images stored in the documents directory) have you any examples or experiences with this in Swift, as well as best practices/approaches please? :) Any tips appreciated! – Phil Hudson Apr 16 '16 at 07:44
0

The keyword is Inter-App Communication. One straightforward way would be to write a URL scheme handler.

From: iPhone OS Programming Guide

Apps communicate only indirectly with other apps on a device. You can use AirDrop to share files and data with other apps. You can also define a custom URL scheme so that apps can send information to your app using URLs.

Note: You can also send files between apps using a UIDocumentInteractionController object or a document picker. For information about adding support for a document interaction controller, see Document Interaction Programming Topics for iOS. For information about using a document picker to open files, see Document Picker Programming Guide.

https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/Inter-AppCommunication/Inter-AppCommunication.html

Rocki
  • 363
  • 1
  • 2
  • 7