A newer version of this documentation is available.

View Latest

Install Couchbase Lite on Swift

      +

      Description — Installing Couchbase Lite on Swift

      Get Started

      To get started coding Couchbase Lite for Swift apps, just create or open an existing Xcode project and install the required Couchbase Lite version, for example 3.1.9, using one of the Install Methods methods shown.

      Couchbase Lite is distributed as an XCFramework, to support this:

      • The minimum required CocoaPods version is 1.9

      • The minimum required Carthage version is 38.0

      Install Methods

      • Direct Download

      • Carthage

      • CocoaPods

      • Swift Package Manager

      1. Download the binaries from here — Extend with Mobile.

      2. Unpack the download

      3. Drag CouchbaseLiteSwift.xcframework from your Finder to the Xcode navigator.

      4. Click on Project  General  Embedded Binary and add CouchbaseLiteSwift.xcframework to this section.

      5. Import the XCFramework

        import CouchbaseLiteSwift
        ...
      6. Start using Couchbase Lite on Swift in your project.

      The minimum required Carthage version is 38.0+
      1. Install Carthage using the instructions here:
        https://github.com/Carthage/Carthage#installing-carthage

      2. In your Cartfile, add the appropriate 'binary' URL:

        Couchbase Lite Community Edition
        binary "https://packages.couchbase.com/releases/couchbase-lite-ios/carthage/CouchbaseLite-Community.json" ~> 3.1.9
        1 Specify the required version number, here we use the latest version
        Couchbase Lite Enterprise Edition
        binary "https://packages.couchbase.com/releases/couchbase-lite-ios/carthage/CouchbaseLite-Enterprise.json" ~> 3.1.9 (1)
        1 Specify the required version number, here we use the latest version
      3. Run carthage update --platform ios.

      4. Drag CouchbaseLiteSwift.xcframework from Carthage/Build/ to the Xcode navigator.

      5. Select Project  General  Embedded Binary, add CouchbaseLiteSwift.xcframework to this section.

      The minimum required Cocoapods version is 1.9
      1. Install CocoaPods using the instructions here:
        https://guides.cocoapods.org/using/getting-started.html

      2. Add one of these targets to your Podfile:

        Couchbase Lite Community Edition
        target 'Example' do
          use_frameworks!
          pod 'CouchbaseLite-Swift', '3.1.9' (1)
        end
        1 Specify the required version number, here we use the latest version
        Couchbase Lite Enterprise Edition
        target 'Example' do
          use_frameworks!
          pod 'CouchbaseLite-Swift-Enterprise', '3.1.9' (1)
        end
        1 Specify the required version number, here we use the latest version
      3. Install the pods and open the .xcworkspace file generated by CocoaPods, using:

        pod install
      Using Swift Package Manager to install CouchbaseLiteSwift requires Xcode 12+

      This tab explains how to include the CouchbaseLiteSwift package within your app
      See: Use Case 1. Include in Existing Swift Package | Use Case 2. Include in Your App Project

      Use Case 1. Include in Existing Swift Package

      Here we will add the CouchbaseLiteSwift dependency to your Parent Swift package — see: Example 1 for the sample manifest.

      1. Add the CouchbaseLiteSwift package as dependency by including the following in the parent package manifest:

        dependencies: [
          .package(name: "CouchbaseLiteSwift",
            url: "insert Couchbase Lite URL", (1)
            from: "3.1.9"), (2)
          ],
        1 Insert appropriate Couchbase Lite URL:
        For Community Edition use: https://github.com/couchbase/couchbase-lite-ios.git
        For Enterprise Edition use: https://github.com/couchbase/couchbase-lite-swift-ee.git
        2 Include the required version number here, for example the latest version is 3.1.9
      2. Add the dependent package product name, to the target:

        targets: [
          .target(name: "ParentPackage",
            dependencies: ["CouchbaseLiteSwift"]),
          ]
      3. Import CouchbaseLiteSwift, and use it:

        import CouchbaseLiteSwift
        
        class ParentPackageSomeClass {
          func someFunction() {
            let db = try! Database(name: "testdb")
            print(">> opening the db: \(db.name)")
          }
        }
      Example 1. Simple Manifest
      // swift-tools-version:5.3
      import PackageDescription
      let package = Package(
      name: "ParentPackage",
      products: [
        .library(
          name: "ParentPackage",
          targets: ["ParentPackage"]),
        ],
      dependencies: [
        .package(name: "CouchbaseLiteSwift",
          url: "https://github.com/couchbase/couchbase-lite-swift-ee.git", from: "3.1.9"),
        ],
      targets: [
        .target(
          name: "ParentPackage",
          dependencies: ["CouchbaseLiteSwift"]),
        .testTarget(
          name: "ParentPackageTests",
          dependencies: ["ParentPackage"]),
        ]
      )
      Use Case 2. Include in Your App Project

      Here we will add CouchbaseLiteSwift directly into your app

      1. Open the project to which you are going to add CouchbaseLiteSwift

        spm 1
      2. Open the Project Editor to add a dependency

        1. In Project Navigator:
          Select your Xcode project file (for example, HostApp in the example)
          Xcode opens the Project Editor pane

        2. In the Project Editor pane:
          Select Project  Swift Packages and + to add the dependency
          Xcode opens the Choose Package Repository dialog

          spm 2
      3. In the Choose Package Repository dialog:
        Enter the appropriate Couchbase Lite URL, Next to continue
        For example: https://github.com/couchbase/couchbase-lite-swift-ee.git

        spm 3
      4. Enter the required Version (the latest is 3.1.9) and Next to continue

        spm 4
      5. Finish to close the Choose Package Repository dialog

        spm 5

        Xcode displays the name, version and URL of the added CouchbaseLiteSwift Package

        spm 6
      6. You can now import CouchbaseLiteSwift, and use it in your app

        spm 7