React Native 0.73.6集成react-native-video iOS构建失败:找不到RCTEventDispatcher
解决React Native 0.73.6 + react-native-video 6.5.0 iOS构建报错:Cannot find type 'RCTEventDispatcher' in scope
问题原因
React Native 0.70+版本中,RCTEventDispatcher已被废弃并移除,而react-native-video ^6.5.0是适配旧版RN的版本,未兼容RN 0.73的API变更,导致构建时找不到该类型。
解决方案
方案一:升级react-native-video到兼容版本
这是最稳妥的解决方式,直接升级到支持RN 0.73的版本(推荐7.x及以上):
- 卸载旧版本:
npm uninstall react-native-video
- 安装最新兼容版本:
npm install react-native-video@latest
- 重新安装Pods:
cd ios && pod install && cd ..
- 清理缓存后重新编译:
# 清理Xcode缓存 rm -rf ~/Library/Developer/Xcode/DerivedData # 清理Metro缓存 npx react-native start --reset-cache
方案二:手动修改react-native-video原生代码(不推荐,适配成本高)
如果暂时无法升级版本,可手动修改react-native-video的iOS原生代码替换废弃API:
- 打开
node_modules/react-native-video/ios/Classes/RCTVideo.swift文件 - 替换所有
RCTEventDispatcher相关代码:- 将属性声明
private var _eventDispatcher: RCTEventDispatcher?替换为:private var _eventEmitter: RCTEventEmitter? - 修改初始化方法:
init(eventEmitter: RCTEventEmitter!) { super.init(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) ReactNativeVideoManager.shared.registerView(newInstance: self) #if USE_GOOGLE_IMA _imaAdsManager = RCTIMAAdsManager(video: self, pipEnabled: isPipEnabled) #endif _eventEmitter = eventEmitter #if os(iOS) if _pictureInPictureEnabled { initPictureinPicture() _playerViewController?.allowsPictureInPicturePlayback = true } else { _playerViewController?.allowsPictureInPicturePlayback = false } #endif } - 找到原代码中
_eventDispatcher?.sendEvent(withName: ...)的调用,替换为:_eventEmitter?.sendEvent(withName: <事件名>, body: <事件参数>)
- 将属性声明
- 修改
RCTVideoManager.swift中对应的初始化调用,将传入的eventDispatcher改为eventEmitter - 重新安装Pods并编译
注:手动修改node_modules代码存在后续npm install被覆盖的风险,建议配合patch-package工具保存修改:
# 安装patch-package npm install patch-package --save-dev # 修改代码后生成补丁 npx patch-package react-native-video
内容的提问来源于stack exchange,提问作者Andrii Spivak
相关产品推荐
相关产品推荐

