Unable to import local XCFramework module

Our project is split up into multiple Pods with CocoaPods and I'm currently trying to convert all of our podspecs to SPM. One of our projects has XCFramework dependencies that I'm trying to include from a local source, but my project files are complaining that the modules couldn't be found when I try to import them. Is there something I'm missing here? My Package.swift file looks something like this:

Code Block
// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
    name: "MyPackage",
    defaultLocalization: "en_us",
    platforms: [
        .iOS(.v12),
        .watchOS(.v6),
        .macOS(.v10_15)
    ],
    products: [
        .library(
            name: "MyPackage",
            targets: [
                "MyPackage"
            ]),
    ],
    dependencies: [
    ],
    targets: [
        .target(
            name: "MyPackage",
            dependencies: [
                .byName(name: "LocalXCFramework")
            ],
            path: "Sources/"
        ),
        
        .binaryTarget(
            name: "LocalXCFramework",
            path: "Frameworks/LocalXCFramework.xcframework"
        )
        
    ]
)


In this example, if I try to import the modules from LocalXCFramework, I get an error saying it couldn't be found.

Replies

This looks like a bug to me, would you mind filing a feedback report?
Sure thing, just filed one! FB7790145
Thank you!
This is the same issue I’m having here:

https://developer.apple.com/forums/thread/651258

will watch this thread
Hi, is there any update for this issue? I'm not able to follow the feedback report since it's private. binaryTarget is still broken with local xcframeworks for me.

Any updates here? This still seems to be an issue

This page may be helpful https://developer.apple.com/documentation/swift_packages/distributing_binary_frameworks_as_swift_packages.

Post not yet marked as solved Up vote reply of yw_v Down vote reply of yw_v
Add a Comment

Is there any workaround for this issue?

There is a workaround by defining a dummy C target that includes the XCFramework. This is explained on the forum: https://forums.swift.org/t/swift-package-and-xcframework-target-for-c-library-where-to-include-the-header/51163/6

You will find a link to a sample repository that show how to architecture the project: https://github.com/withuno/UnoSwift

// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "IBANKit",
    products: [
        // Products define the executables and libraries a package produces, making them visible to other packages.
        .library(
            name: "IBANKit",
            targets: ["IBANKit"]),
    ],
    dependencies: [],
    targets: [
       // Adds the C module as a dependencies to your C sources
        .target(
            name: "IBANKit", 
            dependencies: ["C"],
            path: "Sources/IBANKit"
        ),
        // Create a dummy c package that uses the XCFramework
        .target(
            name: "C",
            dependencies: ["IBANChecker-Core"],
            path: "Sources/C"
        ),
        .binaryTarget(name: "IBANChecker-Core", path: "Libs/IBANChecker-Core.xcframework"),
    ]
)

Then create a dummy .c source:

/// Sources/C/bridge.c
#include "bridge.h"

void __dummy() {}

and a .h in:

// Sources/C/include/bridge.h
#define IBANKIT_VERSION 1

/* Here include the header of the XCFramework */
#include "IBANChecker-Core.h"

And finally in your swift file

import C

// Now uses symbols defined in the XCFramework