如何为iOS 13以下版本排除不兼容的Flutter插件?
flutter_nfc_kit 低版本iOS兼容排除配置方案
flutter_nfc_kit 依赖iOS 13以上系统提供的CoreNFC接口,要在iOS13以下设备上不加载该插件、避免启动崩溃,按以下步骤配置即可:
- 修改iOS端Podfile配置
打开项目iOS目录下的Podfile,先设置项目兼容的最低iOS版本(低于13.0即可,按你项目实际需求填),再在post_install钩子中单独给flutter_nfc_kit设置部署版本,同时配置CoreNFC弱链接:
- 修改iOS端Podfile配置
# 替换为你项目需要兼容的最低iOS版本,比如11.0、12.0 platform :ios, '11.0' post_install do |installer| installer.pods_project.targets.each do |target| flutter_additional_ios_build_settings(target) # 单独指定flutter_nfc_kit的最低部署版本为13.0 if target.name == 'flutter_nfc_kit' target.build_configurations.each do |config| config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '13.0' # 配置弱链接CoreNFC,低版本系统不会因找不到框架符号崩溃 config.build_settings['OTHER_LDFLAGS'] = ['$(inherited)', '-weak_framework', 'CoreNFC'] end end end end
- 调整Info.plist权限配置
打开ios/Runner/Info.plist文件,保留NFC相关权限声明即可——因为前面已经做了框架弱链接,低版本系统不会触发NFC权限校验,注意把权限文案替换成你应用实际的使用说明:
- 调整Info.plist权限配置
<key>NFCReaderUsageDescription</key> <string>用于读取NFC卡片信息完成业务操作</string> <key>com.apple.developer.nfc.readersession.iso7816.select-identifiers</key> <array> <string>A0000000031010</string> <string>A000000003101001</string> <!-- 按需添加你业务需要的AID --> </array>
- Dart层加版本拦截逻辑
不要在应用启动时直接初始化flutter_nfc_kit,先判断当前设备系统版本,仅iOS 13及以上版本才调用插件相关逻辑,从代码层避免低版本加载插件:
- Dart层加版本拦截逻辑
import 'dart:io'; /// 检测当前设备是否支持NFC能力 Future<bool> checkNfcSupport() async { if (Platform.isIOS) { // 提取iOS主版本号 final systemVersion = Platform.operatingSystemVersion; final mainVersionStr = systemVersion.split(' ')[1].split('.').first; final mainVersion = int.tryParse(mainVersionStr) ?? 0; if (mainVersion < 13) { return false; } // iOS13及以上再导入插件、初始化NFC能力 // final nfcAvailable = await FlutterNfcKit.nfcAvailability; // return nfcAvailable == NFCAvailability.available; } // Android端兼容逻辑自行处理 return true; }
- 清理缓存重新构建
配置完成后依次执行以下命令,清除旧的构建缓存重新生成依赖:
- 清理缓存重新构建
flutter clean flutter pub get cd ios && pod install --repo-update
注意:如果配置后低版本设备仍出现启动崩溃,可打开Xcode找到Runner项目的Build Settings,在Other Linker Flags中手动添加
-weak_framework CoreNFC,确保CoreNFC为可选加载状态。配置完成后iOS13以下设备不会加载flutter_nfc_kit插件,NFC功能仅对满足版本要求的用户开放即可。
内容的提问来源于stack exchange,提问作者Didjey
相关产品推荐
相关产品推荐

