Search code examples
swiftcocoapodsswift-package-managergrdb

Adding minimal GRDB to an Xcode project using Swift Package Manager, Cocoa Pods, or other


The demo iOS application from GRDB on github runs great on the phone simulator of my iMac, but it's not clear how to get GRDB building in an iOS application of my own (part 1 of this question), with the minimal set of components (part 2 of this question).1

The GRDB installation instructions suggests offers as one option: "Swift Package Manager". Since I'm new to this programming environment, this sounded like "the right way", but after trying it, I was still left guessing at how to get my project building successfully.

Steps done so far:

  • Created a new project Applications > Xcode (9.4 beta) > Create a new Xcode project > Single View App > "FirstDb"
  • Put import GRDB in the view controller (obviously couldn't compile yet)
  • Applications > Utilities > Terminal
    • cd /Users/owner/documents/xcodeprojects/firstdb
    • mkdir GRDB
    • cd GRDB
    • swift package init --type library
  • Edit Package.swift, adding .package(url: "https://github.com/groue/GRDB.swift.git", from: "2.10.0") at the appropriate location
  • Applications > Utilities > Terminal
    • swift package resolve (resulting in 'Fetching', 'Cloning', 'Resolving')

Status After Product > Build, the import GRDB line still says no such module.

So part 1 of the question is still unresolved. I have not been able to address part 2 of the question yet. Sorry if either or both of these are brutally obvious to the well heeled Xcode developer, but after researching the problem, I found no specific guidance.

Using SPM is not a requirement, so if the other options to integrate GRDB are a better choice, I'd like to see how to integrate using one of those.

Footnote

  1. I would like to understand the steps required to add only the portions of GRDB that are required to use the Record object and to be able to execute SQL statements for an iOS project. The presumption is that much of the complete package (tests, watch, etc, is not required in the project using the GRDB basics, resulting in a more compact iOS application.

Solution

  • Cocoa Pods

    As indicated by the author, Swift Package Manager is only one option and is "not known to integrate with existing Xcode projects", so probably not a good choice. So to address part one of the question, the technique for integrating GRDB using Cocoa Pods is shown below.

    Cocoa Pods Install

    Based on this tutorial, you should be able to install CocoaPods without downloading anything first if your OS is OS X 10.7 or newer.

    • Applications > Utilities > Terminal
      • sudo gem install cocoapods
      • pod setup --verbose

    My install seemed to work fine, but generated more logging than the tutorial showed.

    Integration with Existing Project

    • Created a new project Applications > Xcode (9.4 beta) > Create a new Xcode project > Single View App > "FirstDb"
    • This created /Users/owner/documents/xcodeprojects/FirstDb/FirstDb.xcodeproj
    • Put import GRDB in the view controller (obviously couldn't compile yet)
    • Close Xcode! (we will be accessing it later, through xcworkspace, not xcodeproj)
    • Applications > Utilities > Terminal
      • cd /Users/owner/documents/xcodeprojects/firstdb
      • pod init
      • open -a Xcode PodFile

    Make your file specify GRDB:

    platform :ios, '9.0'
    target 'FirstDb' do
        use_frameworks!
        pod 'GRDB.swift'
    end
    

    That doesn't take advantage of the versioning function available, so it will be up to you to make sure there's no breaking changes by introduced by GRDB as it is enhanced.

    Use Cocoa Pods to get GRDB

    Now is when you download GRDB:

    • Applications > Utilities > Terminal
      • cd /Users/owner/documents/xcodeprojects/firstdb
      • pod install

    You should see something like this:

    Analyzing dependencies
    Downloading dependencies
    Installing GRDB.swift (2.10.0)
    Generating Pods project
    Integrating client project
    [!] Please close any current Xcode sessions and use `FirstDb.xcworkspace` for this project from now on.
    ....
    

    Build the Project with GRDB import

    Now open the workspace file FirstDb.xcworkspace (not the proj file). In the left project outline, you should see your starter project and also Pods:

    FirstDb
    Pods
    

    Build the workspace (Product > Build), and you should see that your import GRDB line in the ViewController is compiling without errors.