Flutter插件包创建失败求助:iOS端Swift版本识别异常
I’ve run into this exact issue before—let’s break down how to fix the Swift version error, plus answer your question about the example directory.
1. Resolve the Swift Version CocoaPods Error
The problem isn’t with your main project’s Podfile—it’s that your Flutter plugin’s iOS component doesn’t explicitly declare its Swift version, so CocoaPods can’t resolve it during integration. Here are two solid fixes:
Option 1: Set Swift Version via Xcode
- Open your plugin’s iOS project: Navigate to
my_plugin/ios/my_plugin.xcodeprojand double-click to launch it in Xcode. - Select the
my_plugintarget (not the top-level project) in the left sidebar. - Go to the Build Settings tab, search for
Swift Language Version. - Set it to a stable version compatible with your Flutter setup (I recommend
5.0—it works with most modern Flutter projects). - Save changes and close Xcode.
Option 2: Update the Plugin’s Podspec File
- Open
my_plugin/my_plugin.podspecin a text editor. - Inside the
Pod::Spec.new do |s|block, add this line:s.swift_version = '5.0' - Save the file.
Clean Caches to Apply Changes
After setting the Swift version, clear cached files to ensure everything updates properly:
- In your main Flutter project, run:
flutter clean - Navigate to your main project’s
iosdirectory and run:pod deintegrate && pod install
Try running your project again—the Swift version error should be resolved.
2. Can I Delete the Plugin’s example Subdirectory?
Absolutely. The example folder is just a pre-built test project generated by Flutter to help you validate your plugin’s functionality during development.
- If you’ve already tested the plugin thoroughly in your main project, deleting
examplewon’t impact the plugin’s integration or core functionality—all critical plugin code lives in the rootmy_plugindirectory. - That said, it’s a good idea to keep it if you plan to maintain or update the plugin later, as it provides a quick, isolated environment to test changes without affecting your main project.
内容的提问来源于stack exchange,提问作者nbloqs

