Home > Blockchain >  Importing pod Framework with same name as Apple Framework
Importing pod Framework with same name as Apple Framework

Time:09-12

I've been using Charts pod since iOS 14 for my app and now I want to migrate to iOS16 Charts that Apple provides. So if the user's device is prior to iOS 16 then the pod's framework should be used otherwise the Apple's one. However, when I try to "import Charts", the one selected is always the one from Pods, in any class or file. They are both named "Charts" and I have no selection over which one. Is there any way to approach this?

CodePudding user response:

CocoaPods just uses the podspecs of your dependencies to generate the Pods Xcode project and for xcode projects PRODUCT_MODULE_NAME build settings govern what the name should be used when trying to import the module built by a target. So you can customize this settings to provide different name to the Charts pod or ask the pod developer to change the module name.

Pod authors can provide custom module name without changing the pod name with module_name property.

To customize the module name yourself you can add a post_install hook that provides this custom name:

post_install do |installer|
  installer.pods_project.targets.each do |target|
    next if target.product_name != 'Charts'
    target.build_configurations.each do |config|
      config.build_settings['PRODUCT_MODULE_NAME'] = 'CustomCharts'
    end
    break
  end
end

And in your code you can import using this custom name i.e. import CustomCharts, to use the CocoPods one.

For Swift packages

With swift 5.7 you can use module aliases to disambiguate modules with same name:

 targets: [
  .executableTarget(
    name: "App",
    dependencies: [
     .product(name: "Charts", package: "Charts", moduleAliases: ["Charts": "CustomCharts"]), 
   ])
 ]
  • Related