You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter插件包创建失败求助:iOS端Swift版本识别异常

Fixing CocoaPods Swift Version Error When Integrating Flutter Plugin (Kotlin/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.xcodeproj and double-click to launch it in Xcode.
  • Select the my_plugin target (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.podspec in 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 ios directory 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 example won’t impact the plugin’s integration or core functionality—all critical plugin code lives in the root my_plugin directory.
  • 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 05:15:19