首页 > 解决方案 > 构建目标 MyNotificationService:找不到框架

问题描述

我有一个 React Native 应用程序“MyApp”,它附加了一个“MyNotificationService”(即NotificationService.appex嵌入Frameworks, Libraries and Embedded Content in Xcode在目标“MyApp”下)。

到目前为止,一切都很好。

现在我想在“MyApp”命名中添加另一个 pod pod 'Google-Mobile-Ads-SDK',这与MyNotificationService. 我只想安装它,所以我可以访问Google-Mobile-Ads-SDK“MyApp”。

我的 Podfile 看起来像这样

target 'MyApp' do
  
  use_unimodules!
  config = use_native_modules!
  use_react_native!(
    :path => config[:reactNativePath],
    :hermes_enabled => false
  )

  # Additional Dependencies
  ...
  pod 'Google-Mobile-Ads-SDK'
 
  target 'MyNotificationService' do   
    use_native_modules!
    inherit! :complete
  end

  post_install do |installer|
    installer.pods_project.targets.each do |target|
      target.build_configurations.each do |config|
        config.build_settings['APPLICATION_EXTENSION_API_ONLY'] = 'NO'
        # deletes each pods deployment target definition and instead uses the definition of the main project
        config.build_settings.delete 'IPHONEOS_DEPLOYMENT_TARGET'
      end
    end
  end
end

但是,在我运行之后pod install,我尝试构建MyApp使用 XCode 的调试版本,但 XCode 抱怨:

Build Target MyNotificationService
Link MyNotificationService (arm64)

Directory not found for option '-F/Users/.../Library/Developer/Xcode/DerivedData/MyApp-abcabcabcabcabc/Build/Products/Debug-iphoneos/XCFrameworkIntermediates/GoogleAppMeasurement'

Directory not found for option '-F/Users/.../Library/Developer/Xcode/DerivedData/MyApp-abcabcabcabcabc/Build/Products/Debug-iphoneos/XCFrameworkIntermediates/GoogleMobileAds'

Directory not found for option '-F/Users/.../Library/Developer/Xcode/DerivedData/MyApp-abcabcabcabcabc/Build/Products/Debug-iphoneos/XCFrameworkIntermediates/UserMessagingPlatform'

但是 MyNotificationService 与这些 Frameworks 无关GoogleAppMeasurementGoogleMobileAds并且UserMessagingPlatform- 那些属于Google-Mobile-Ads-SDK并且只有主要目标MyApp应该需要它们。

我怎么知道MyNotificationService它不应该关心这些框架?

标签: iosxcodereact-nativecocoapodsios-app-extension

解决方案


确实使用抽象目标解决了问题,其中定义了“MyApp”和“MyNotificationService”作为子目标。抽象目标不是实际的 Xcode 目标,而是一个占位符或文件夹,它定义了“MyApp”和“MyNotificationService”都应该共享的 pod。要获得“MyNotificationService”,请忽略与“Google-Mobile-Ads-SDK”相关的所有内容,pod 'Google-Mobile-Ads-SDK'仅在子目标“MyApp”中定义。

abstract_target 'Shared' do
  
  use_unimodules!
  config = use_native_modules!
  use_react_native!(
    :path => config[:reactNativePath],
    # to enable hermes on iOS, change `false` to `true` and then install pods
    :hermes_enabled => false
  )

  # Additional Dependencies
  ...

  target 'MyApp' do
    # no inherit statement means that target MyApp has all pods from the parent "Shared" accessible
    pod 'Google-Mobile-Ads-SDK'
  end

  target 'MyNotificationService' do 
      # no inherit statement means that target MyNotificationService has all pods from the parent "Shared" accessible
    use_native_modules!
  end
 
  post_install do |installer|
    installer.pods_project.targets.each do |target|
      target.build_configurations.each do |config|
        config.build_settings['APPLICATION_EXTENSION_API_ONLY'] = 'NO'
        # deletes each pods deployment target definition and instead uses the definition of the main project
        config.build_settings.delete 'IPHONEOS_DEPLOYMENT_TARGET'
      end
    end
  end
end

推荐阅读