feat(ios): Add Swift Package Manager (SPM) support#235
Conversation
Adds a Package.swift manifest for the iOS plugin so apps using Flutter's Swift Package Manager integration can resolve mixpanel_flutter natively instead of falling back to CocoaPods. Closes mixpanel#176 Changes: - ios/mixpanel_flutter/Package.swift: SPM manifest declaring a single pure-Swift target that depends on Mixpanel-swift 6.3.0 (matches the existing podspec dependency exactly). - ios/mixpanel_flutter/Sources/mixpanel_flutter/: Swift sources moved into the SPM-expected layout. Files are symlinks back to the canonical shared sources in swift/Classes/, mirroring the pattern already used by macos/Classes/. - ios/mixpanel_flutter.podspec: source_files path updated to point at the new Sources directory. CocoaPods consumers are unaffected. - swift/Classes/SwiftMixpanelFlutterPlugin.swift: added @objc(MixpanelFlutterPlugin) to expose the Swift class to Obj-C registrants under the same name as the previous wrapper. This preserves backwards compatibility for projects with an Obj-C AppDelegate. - ios/Classes/: removed. The Obj-C wrapper (.h/.m) is no longer needed since the Swift class is now @objc-exposed under the expected name. The old Swift symlinks here are replaced by the equivalent ones under the new Sources/ layout. macOS is unchanged (Pod-only) — left for a follow-up.
|
Thanks for the detailed PR @TheoGermain, really appreciate the writeup and the verification checklist. I'll review and update here. |
|
Hi @TheoGermain, I tried running the example app with SPM enabled and the build is failing with the error below. Can you confirm if it works on your end? Thanks! |
…RL identity SPM derives a package's identity from the directory name for local path dependencies. The repository URL is `github.com/mixpanel/mixpanel-flutter` (hyphen), so the expected identity is `mixpanel-flutter`. Flutter's SPM integration was pointing at the `mixpanel_flutter` directory (underscore), causing an identity mismatch: unable to override package 'mixpanel_flutter' because its identity 'mixpanel-flutter' doesn't match override's identity (directory name) 'mixpanel_flutter' Changes: - Rename ios/mixpanel_flutter/ → ios/mixpanel-flutter/ - Update Package.swift name: "mixpanel_flutter" → "mixpanel-flutter" - Add swiftPackageName: mixpanel-flutter to pubspec.yaml iOS platform - Update podspec source_files path to match new directory name
|
Hi @ketanmixpanel, good catch — thank you for testing! The root cause is an SPM package identity mismatch. SPM derives a package's identity from the directory name for local path overrides. Flutter was pointing at the I just pushed a fix (aae8970) that makes three changes:
The |
Closes #176.
Summary
Adds a
Package.swiftmanifest for the iOS plugin so apps using Flutter'sSwift Package Manager integration can resolve
mixpanel_flutternativelyinstead of falling back to CocoaPods. CocoaPods consumers are unaffected —
the
.podspecstill works and is unchanged in semantics.The scope of this PR is iOS only. macOS still uses CocoaPods; SPM support
for macOS can be added in a follow-up since it is largely the same pattern.
What changed
ios/mixpanel_flutter/Package.swiftmixpanel-swift6.3.0 (pinned to the same exact version as the podspec). iOS 12.0 minimum (matches the podspec).ios/mixpanel_flutter/Sources/mixpanel_flutter/swift/Classes/, mirroring the pattern already used bymacos/Classes/.ios/mixpanel_flutter.podspecsource_filesupdated to point at the newSources/directory.swift/Classes/SwiftMixpanelFlutterPlugin.swift@objc(MixpanelFlutterPlugin)to expose the Swift class to Obj-C registrants under the same name as the previous Obj-C wrapper. This preserves backwards compatibility for projects with an Obj-CAppDelegate.ios/Classes/.h/.m) is no longer needed since the Swift class is now@objc-exposed under the expected name. The old Swift symlinks here are replaced by the equivalent ones under the newSources/layout.Why eliminate the Obj-C wrapper
The previous
MixpanelFlutterPlugin.{h,m}only existed to bridge fromObj-C-named
MixpanelFlutterPlugin→ SwiftSwiftMixpanelFlutterPlugin.Mixed Swift+Obj-C SPM targets are awkward (SPM does not natively support
mixed-language targets in a single module).
By annotating the Swift class with
@objc(MixpanelFlutterPlugin):AppDelegate.mprojects) still find the class asMixpanelFlutterPlugin— no behavior change.This mirrors the approach already taken on macOS (where a Swift
typealiasserves the same purpose, no Obj-C wrapper).
Verification
flutter pub getsucceeds inexample/with SPM enabled(
flutter config --enable-swift-package-manager)flutter runbuilds and launches on an iOS simulator with SPM enabledflutter runbuilds and launches on an iOS simulator with SPM disabled(CocoaPods path)
pod lib lint ios/mixpanel_flutter.podspecstill passesAppDelegateconsumers still resolveMixpanelFlutterPluginvia the auto-generated registrant
Follow-ups (out of scope here)
macos/).match the existing podspec).
References
mixpanel-swiftalready supports SPM (Mixpanel/Package.swift)