skip to Main Content

I want to write an internal NPM to work with Firebase Firestore, for this I need to use the Firebase SDK via SPM.
I add dependency in Package.swift (example below)
but I can’t use it in code.

My Package.swift code

import PackageDescription

let package = Package(
    name: "FCM",
    platforms: [.iOS(.v16)],
    products: [
        .library(
            name: "FCM",
            targets: ["FCM"]),
    ],
    dependencies: [
        .package(url: "https://github.com/firebase/firebase-ios-sdk", .upToNextMinor(from: "9.6.0")),
    ],
    targets: [
        .target(
            name: "FCM",
            dependencies: [
            ]),
        .testTarget(
            name: "FCMTest",
            dependencies: ["FCMTest"]),
    ]
)

2

Answers


  1. With dependencies: [.package(url: ...)] you are just saying that your Package will depend on the Firebase package.

    To use a dependency package in one of your targets, you need to specify what product your target will need to depend on.

    In your target add the dependencies: [] attribute and define the firebase framework that you need by using .product(name: , package: ):

    let package = Package(
        name: "MyPackage",
        products: [
            .library(
                name: "SomeLibrary",
                targets: ["SomeTarget"]),
        ],
        dependencies: [
            .package(url: "https://github.com/firebase/firebase-ios-sdk", branch: "master")
        ],
        targets: [
            .target(
                name: "SomeTarget",
                dependencies: [
                    // THIS WHAT MISSING
                    .product(name: "FirebaseFirestore", package: "firebase-ios-sdk"),
                ]
            )
        ]
    )
    

    Useful links:

    Login or Signup to reply.
  2. Guiseppe’s answer doesn’t work for me even though it goes straight from the Firebase documentation. I had to change package’s name from Firebase to firebase-ios-sdk:

    let package = Package(
        name: "MyPackage",
        products: [
            .library(
                name: "MyPackage",
                targets: ["MyPackage"])
        ],
        dependencies: [
            .package(url: "https://github.com/firebase/firebase-ios-sdk.git", from: "8.0.0")
    
        ],
        targets: [
            .target(
                name: "Configuration",
                dependencies: [
                    .product(name: "FirebaseAuth", package: "firebase-ios-sdk")
    
                ]
            ),
            .testTarget(
                name: "MyPackageTests",
                dependencies: ["MyPackage"])
        ]
    )
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search