Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a local Swift Package target to an existing Xcode project

I have an existing Xcode project that contains targets for a "traditional" iOS app ("Foo") and an iOS framework ("FooLib"). When the Xcode 12 beta dropped a few days ago, I added a multiplatform (iOS/macOS) SwiftUI app target ("Foo SwiftUI") to the same project. Now I want to make my framework cross-platform, too, so I added a multiplatform Swift Package ("Foo Package") to the project and moved the iOS framework classes to the new Swift package. I then added this package to all app targets' Frameworks, Libraries, and Embedded Content sections. It appeared to work for all app targets until I cleaned the project and tried again. Now the app targets complain that the Foo Package can't be resolved:

Missing package project 'FooLib'.

I tried removing & re-adding the FooLib package to the app targets, but it no longer shows up in the pop-up list of frameworks and libraries to add. Are local Swift packages explicitly unsupported in Xcode projects? Do I need to put the package in its own Git repo and set it up like any other third-party package dependency?

What I'm most confused about it that it looks like it worked right after I created the Swift package, but not after I cleaned the project and tried re-running the app targets.

like image 848
NRitH Avatar asked Oct 25 '25 00:10

NRitH


2 Answers

OK, sorry for my previous answer. — It is obvious how to share a package with yourself by way of a remote URL, such as GitHub, that serves it. Basically you are playing the role of both developer and end-user, and it's clear how to do that. So I think what's needed here is a step-by-step tutorial to sharing a package with yourself locally without actually serving it.

  1. In Xcode, choose File > New > Swift Package and save as MyLibrary to the Desktop.

  2. Now choose File > New > Project, choose the iOS App template, and save as App1 to the Desktop.

  3. You now have two windows open. Drag MyLibrary from the project navigator in the MyLibrary window into the empty area at the bottom of the project navigator in the App1 window.

  4. Quit Xcode.

  5. Launch Xcode again and open App1. Look, the MyLibrary icon now has hierarchical contents. Edit the app target and add MyLibrary library as a dependency.

  6. You can now edit MyLibrary; you can also import MyLibrary in your code and you will be able to use any public members of public types.

  7. Close App1 and create another project, App2. Repeat steps 3, 4, 5, and 6.

From now on, either App1 and App2 can "see" MyLibrary. From either one, you can edit MyLibrary, you can import it and use its code. But you cannot have both App1 and App2 open at the same time. If you do, one of them will complain that the package is missing. So you'll be fine as long as you only have one at a time open.

Now, if you want to escape that limitation, then go back to the first way of dealing with all this: upload the package to GitHub and acquire it in your projects as a package dependency.

like image 169
matt Avatar answered Oct 26 '25 16:10

matt


For me, it works using Xcode Workspaces “around” local Projects and Swift Packages.

  • Create App project
  • Create Swift Package
  • Create Workspace for app project
  • Add the App project to the Workspace
  • Add the Swift Package to the Workspace, too
  • In the app target “General” settings, “embed&sign” the Swift Package from the current workspace

You might have to restart Xcode afterwards for it to pick up on the changes ...

like image 25
lsrggr Avatar answered Oct 26 '25 17:10

lsrggr