skip to Main Content

I am trying to create a Xcode workspace which has multiple projects in it. I want to add different dependencies for each project. Let’s say I have MyApp (Master project), WebService (framework) and UIComponent (framework) projects. I wan’t to add Alamofire only to WebService project or I wan’t to add Lottie for only to UIComponent project.

MyWorkspace
 |- MyApp
 |- WebService
    |-> Alamofire
 |- UIComponent
    |-> Lottie

The problem is, when I add WebService and UIComponent dependency to my MyApp project (via Linking Binaries from Xcode), it can access dependencies of my sub-projects. In other words MyApp project can access and import Alamofire and Lottie. Is there a way to prevent MyApp project can not import sub-projects dependencies?

I tried to achieve this using both Swift Package Manager and CocoaPods but no luck.

2

Answers


  1. CocoaPods is fine, distinguish different target

    #targetA: [AFNetworking,Masonry]
    #targetB:[AFNetworking,SDWebImage]
    abstract_target 'abstract_pod' do 
        pod 'AFNetworking'
        
        target 'targetA' do
            pod 'Masonry' 
        end
        
        target 'targetB' do
            pod 'SDWebImage' :path =>'./SDWebImage'
        end
    end
    
    Login or Signup to reply.
  2. With Swift Package Manager

    You actually don’t need a workspace to do this. You could just use an Xcode Project. I am currently doing this for a project I’m working on.

    RootProjectFolder
     - Core // Swift Package
     - DataAccess // Swift Package
     - Http // Swift Package
     - ApplicationServices // Swift Package
     - Presenters // Swift Package
     - App/
     - AppTests/
     - App.xcodeproj
    

    Core is used in every package, Http in DataAccess, DataAccess in ApplicationServices, ApplicationServices in Presenters, and Presenters in the app.

    To create a setup like this, open your workspace or Xcode project click file and create a new swift package. Save that package in your application’s directory and link it to the project or workspace you want to work in.

    Swift package creation

    If you do this a few times you can start referencing the other local swift packages like this:

    
    let package = Package(
        name: "ApplicationServices",
        products: [.library(
                name: "ApplicationServices",
                targets: ["ApplicationServices"]),
        ],
        dependencies: [
            // NOTE that we can use a relative path to link  
            // to include the other local packages. 
            .package(name: "DataAccess", path: "../DataAccess"),
            .package(name: "Core", path: "../Core"),
            // Or the traditional way of pulling down a remote package
            .package(url: "https://github.com/some-swift/testing-package", .upToNextMajor(from: "9.0.0"))
    
        ],
        targets: [
            .target(
                name: "ApplicationServices",
                // This is where you tell the swift package to use a dependency
                dependencies: ["DataAccess", "Core"]), the
            .testTarget(
                name: "ApplicationServicesTests",
                dependencies: ["ApplicationServices", "DataAccess", "Core", "TestingPackage"]),
        ]
    )
    

    Then with a local package, we include it a little differently than a remote swift package. Click on the Project in the file navigation, select the app target, then under General in Frameworks, Libraries, and Embedded Content click the + button. You should be able to select the local packages you care about.

    If you need anything described more clearly let me know.

    Adding local packages

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search