Skip to content

Instantly share code, notes, and snippets.

@cdeil
Created February 18, 2026 20:41
Show Gist options
  • Select an option

  • Save cdeil/020243971d330ac23979fec66b766ac1 to your computer and use it in GitHub Desktop.

Select an option

Save cdeil/020243971d330ac23979fec66b766ac1 to your computer and use it in GitHub Desktop.

Swift Package Manager Support for printing Plugin

I have implemented full Swift Package Manager (SPM) support for the printing package on iOS and macOS, following the official Flutter guidelines and addressing the limitations of mixed-language targets in SPM.

Implementation Details

1. Package.swift Creation

I created printing/ios/Package.swift and printing/macos/Package.swift. These manifests define the library targets and dependencies.

2. Handling Mixed Language Limitations

The printing plugin originally used a mixed Objective-C (PrintingPlugin.m) and Swift (PrintingPlugin.swift) codebase in the same target.

  • Problem: Swift Package Manager strictly prohibits mixed language sources in a single target unless they are separated into distinct targets with public headers, which adds complexity and potential linking issues for existing plugins.
  • Solution: I utilized the @_cdecl attribute in Swift to expose the C-compatible symbols directly from the Swift code, effectively removing the need for PrintingPlugin.m when building with SPM.

3. Changes Applied

  • Modified PrintingPlugin.swift (iOS & macOS):

    • Added @_cdecl wrapper functions (net_nfet_printing_set_document, net_nfet_printing_set_error) at the end of the file.
    • Wrapped these functions in #if SPM_BUILD to ensure they are only compiled when building with SPM. This prevents duplicate symbol errors for CocoaPods builds (which still compile PrintingPlugin.m).
  • Updated Package.swift (iOS & macOS):

    • Added exclude: ["PrintingPlugin.m"] to the target definition. This ensures the Objective-C file is ignored by SPM, allowing the target to be pure Swift.
    • Added swiftSettings: [.define("SPM_BUILD")] to define the compilation flag that enables the @_cdecl wrappers.

Testing

I verified the changes locally by:

  1. Creating a clean Flutter app (test_spm_app).
  2. Configuring it to depend on the local printing package path.
  3. Enabling SPM in Flutter config (flutter config --enable-swift-package-manager).
  4. Successfully building the app for both iOS (flutter build ios --no-codesign) and macOS (flutter build macos).
  5. Verifying that swift package describe runs successfully on the package manifests.

Open Questions / Notes

  • The current implementation maintains full backward compatibility with CocoaPods (which uses PrintingPlugin.m and ignores the #if SPM_BUILD block).
  • Future cleanup: Once CocoaPods support is dropped (if ever), PrintingPlugin.m can be removed entirely, and the @_cdecl wrappers can be made unconditional.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment