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

Expo自定义模块添加Widget Extension的问题求助

在Expo自定义模块里加iOS Widget Extension的正确操作流程

1. 别在主项目建Widget,去模块原生目录里搞

直接打开你自定义模块的iOS工程(MyModule/ios/MyModule.xcodeproj),通过File > New > Target选Widget Extension,创建时把存储路径设为MyModule/ios/MyWidget,让这个Extension属于模块自己的工程,别扔主项目顶层。

2. 改Podspec让Extension跟着模块走

在MyModule.podspec里加个subspec配置,把Widget的代码、资源都包含进去,让Expo的Pod管理能识别到它:

s.subspec 'MyWidget' do |widget|
  widget.source_files = 'ios/MyWidget/**/*.{swift,h,m}'
  widget.resources = 'ios/MyWidget/**/*.{xcassets,strings,plist}'
  widget.platform = :ios, '14.0'
  widget.frameworks = 'SwiftUI', 'WidgetKit'
  widget.pod_target_xcconfig = {
    'PRODUCT_BUNDLE_IDENTIFIER' => '你的模块Bundle ID.MyWidget',
    'SWIFT_VERSION' => '5.0'
  }
end

# 让主模块依赖这个Widget子spec
s.dependency 'MyModule/MyWidget'

3. 解决_main重复符号问题

别删Widget里的@main!问题是你之前把Widget代码和主模块混编了。要确保Widget是独立的target:

  • 在Xcode里确认MyWidget target和MyModule主target是分开的
  • 检查MyWidget的Main Interface是不是正确指向它自己的入口文件
  • 主模块的Pod配置里别把Widget的源文件包含进去

4. 搞定找不到MyWidgetExtension.appex的问题

  • Bundle ID要对应:Widget的Bundle ID必须是主应用ID的后缀,比如主应用是com.xxx.MyApp,Widget就得是com.xxx.MyApp.MyWidget
  • 配置嵌入Extension:要么在主应用的Xcode target里,去General > Embedded Binaries把MyWidgetExtension加进去;要么在主项目Podfile里加post_install脚本自动处理:
post_install do |installer|
  installer.pods_project.targets.each do |target|
    if target.name == 'MyModule_MyWidget'
      target.build_configurations.each do |config|
        config.build_settings['CODE_SIGN_IDENTITY'] = 'Apple Development'
        config.build_settings['DEVELOPMENT_TEAM'] = '你的开发Team ID'
      end
      # 让主应用依赖这个Widget target
      app_target = installer.pods_project.targets.find { |t| t.name == '你的主应用target名' }
      app_target.add_dependency(target) if app_target
    end
  end
end
  • 清缓存重新编译:先跑pod deintegrate && pod install,再用Cmd+Shift+K清Xcode缓存,然后重新编译

5. 验证效果

编译完成后,模拟器里应该能找到你的Widget,同时TypeScript调用模块的功能也正常。


内容的提问来源于stack exchange,提问作者theredfox

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 21:32:46