Post

Integrate Capacitor into Your Swift Package

Seamlessly add the Capacitor cross-platform framework to your Swift Package, avoiding common errors and dependencies.

Integrate Capacitor into Your Swift Package

Many iOS developers use Capacitor through CocoaPods or by adding it to an Xcode project via Swift Package Manager (SPM). However, a common oversight is not properly integrating Capacitor as a dependency within the SPM configuration.

Integrating Capacitor in Your Swift Package

Let’s look at the standard Swift Package configuration:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// swift-tools-version: 6.0
import PackageDescription

let package = Package(
  name: "MyLibrary",
  products: [
    .library(
      name: "MyLibrary",
      targets: ["MyLibrary"]
    )
  ],
  dependencies: [
    .package(url: "https://github.com/ionic-team/capacitor-swift-pm", from: "7.4.0")
  ],
  targets: [
    .target(
      name: "MyLibrary",
      dependencies: [
        .product(name: "Capacitor", package: "capacitor-swift-pm"),
      ]
    )
  ]
)

This configuration looks correct, but there’s a catch - you need to also include the Cordova dependency alongside Capacitor. Otherwise, you may encounter the following errors:

Error 1: SDK/Compiler Version Mismatch

1
2
3
4
Failed to build module 'Capacitor'; this SDK is not supported by the compiler 
(the SDK is built with 'Apple Swift version 6.0.3 effective-5.10 (swiftlang-6.0.3.1.10 clang-1600.0.30.1)', 
while this compiler is 'Apple Swift version 6.2 effective-5.10 (swiftlang-6.2.0.19.9 clang-1700.3.19.1)'). 
Please select a toolchain which matches the SDK.

Error 2: Clang Dependency Scanner Failure

1
2
3
4
5
6
Clang dependency scanner failure: While building module 'Capacitor' imported from Capacitor-a3da01da.input:1:
In file included from <module-includes>:1:
In file included from /Users/<username>/Library/Developer/Xcode/DerivedData/<project>/Build/Products/Debug-iphonesimulator/Capacitor.framework/Headers/Capacitor.h:13:
/Users/<username>/Library/Developer/Xcode/DerivedData/<project>/Build/Products/Debug-iphonesimulator/Capacitor.framework/Headers/CAPInstanceDescriptor.h:5:9: 
fatal error: module 'Cordova' not found
Capacitor-a3da01da.input:1:1: fatal error: could not build module 'Capacitor'

Error 3: Module Dependency Not Found

1
2
3
Unable to find module dependency: 'Capacitor'
@_exported import Capacitor
^

To prevent these errors, you need to add the Cordova dependency along with Capacitor:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// swift-tools-version: 6.0
import PackageDescription

let package = Package(
  name: "MyLibrary",
  products: [
    .library(
      name: "MyLibrary",
      targets: ["MyLibrary"]
    )
  ],
  dependencies: [
    .package(url: "https://github.com/ionic-team/capacitor-swift-pm", from: "7.4.0")
  ],
  targets: [
    .target(
      name: "MyLibrary",
      dependencies: [
        .product(name: "Capacitor", package: "capacitor-swift-pm"),
        .product(name: "Cordova", package: "capacitor-swift-pm"), // Add this dependency
      ]
    )
  ]
)

Now you can easily build your targets that use Capacitor within your Swift Package.

Practical Takeaways

  • Always include both Capacitor and Cordova dependencies when using Capacitor in a Swift Package
  • Verify your Swift and Xcode versions match the Capacitor SDK requirements to avoid compiler mismatches
  • Use the latest versions of Capacitor and Cordova to ensure compatibility and bug fixes

By following these guidelines, you can seamlessly integrate Capacitor into your Swift Package and avoid common dependency-related errors.


Full list of Capacitor integration resources: https://github.com/ionic-team/capacitor-swift-pm

☕ Support My Work

If you found this post helpful and want to support more content like this, you can buy me a coffee!

Your support helps me continue creating useful articles and tips for fellow developers. Thank you! 🙏

This post is licensed under CC BY 4.0 by the author.