添加watchOS target后出现No such module 'FirebaseFirestore'问题求助
Key Issues
- FirebaseFirestore (and FirebaseFirestoreSwift) does not support watchOS (confirmed in your error for version 10.3.0).
- Shared files used by both iOS and watchOS targets are attempting to import
FirebaseFirestore, which is unavailable in the watchOS build context. - Your Podfile applies identical Firebase pods to both targets, but watchOS can't handle Firestore or its dependent libraries.
Step-by-Step Fixes
1. Split Firebase Pods by Platform
Modify your Podfile to separate Firebase pods into iOS-only and watchOS-only groups, since not all Firebase libraries support watchOS:
def ios_firebase_pods pod 'Firebase/Database' pod 'Firebase/Auth' pod 'Firebase/Storage' pod 'Firebase/RemoteConfig' pod 'Firebase/Firestore' # Re-enable only if your iOS target needs it # pod 'FirebaseFirestoreSwift' # Only include here (iOS-only) if required end def watchos_firebase_pods pod 'Firebase/Auth' # Supported on watchOS pod 'Firebase/RemoteConfig' # Supported on watchOS # Exclude Database, Storage, Firestore—these don't work on watchOS end
Update your target definitions to use the correct pod groups:
target 'HonestMate' do platform :ios, '16.0' use_frameworks! helpers_pods ios_firebase_pods target 'HonestMateTests' do inherit! :search_paths end target 'HonestMateUITests' do end end target 'HonestMate Watch App' do platform :watchos, '9.0' use_frameworks! watchos_firebase_pods end
2. Guard Firestore Code for iOS Only
In any shared files that import FirebaseFirestore, wrap the relevant code with platform conditionals to prevent compilation for watchOS:
#if os(iOS) import FirebaseFirestore // All Firestore-specific logic goes here #endif
Alternatively, if entire files are Firestore-exclusive, remove them from the watchOS target's membership:
- Select the file in the Project Navigator
- Open the File Inspector (right sidebar)
- Uncheck the watchOS target under Target Membership
3. Fix Post-Install Script
Your current script sets the iOS deployment target for all targets, including watchOS. Update it to target only iOS platforms:
post_install do |installer| installer.pods_project.targets.each do |target| target.build_configurations.each do |config| if target.platform.name == :ios config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '16.0' end if target.platform.name == :watchos config.build_settings['WATCHOS_DEPLOYMENT_TARGET'] = '9.0' end end end end
4. Reinstall Pods
Run these commands to clean and refresh your Pod setup:
pod deintegrate pod install
5. Handling FirebaseFirestoreSwift
Since it depends on Firestore (unsupported on watchOS), you can only include it in the iOS target. Uncomment it in ios_firebase_pods and ensure any code using it is wrapped in #if os(iOS) guards.
Verification
- Open the
.xcworkspacefile (never use.xcodeprojwith CocoaPods). - Clean the build folder (
Cmd+Opt+K) and delete Derived Data (Xcode > Settings > Locations > Derived Data > Delete). - Build both iOS and watchOS targets separately to confirm no module errors.
内容的提问来源于stack exchange,提问作者Karina

