'How can I add a demo app target to a Swift Package?

I'm looking to include a demo app with a Swift Package of custom controls that shows implementation demos for each of the controls. Is there a way that I can include this in the Swift Package (and also develop/test the controls directly inside the package this way)?



Solution 1:[1]

What kind of app? SwiftPM only directly supports macOS/Linux executables.

If you want an iOS/tvOS/watchOS one you will need an Xcode project that links using a relative path to find your package. This is done by creating the example app and dragging the Package's containing directory into the Xcode project. You can then link the library. If you put this example app in the repo with the package then it will be distributed along with anyone who clones the package.

One important thing I have noticed (which is almost certainly a bug in Xcode) when doing this myself is that the Package.swift and Example.xcodeproj cannot be in the same directory. You have to nest the Xcode project into another directory or it will have issues building/linking a lot of the time. So if you run into any issues With what I have suggested also try this bandaid.

I have created an example on GitHub here that works for Xcode 11.3.

Solution 2:[2]

The best approach is to use "Xcode workspace". How does it work?

1. Create your artifacts

Create a directory.

Create an Xcode workspace inside there (Xcode: File: New: Workspace). Then create your library (package), and create your app (whatever type) inside the same directory.

In this step, just place the package and the app in the above directory, and close them immediately.

It should look like this.

enter image description here

2. Link your artifacts

Open your .xcworkspace file.

In the left bottom corner, click "+" sign.

enter image description here

Select "Add files to your_workspace_name", then:

a) for the Demo app

Navigate to the app's directory, select Your_app.xcodeproj and hit Add.

b) for the package

Navigate to the package's directory, and select the package directory only. Before hitting Add button, make sure "Create folder references for any added folders" is selected.

enter image description here

Your entire workspace should look like this now. You can immediately import the library inside your app and you are ready to go.

enter image description here

Solution 3:[3]

Here are the steps that worked for me on Xcode 13:

  1. Create a package called MyLibrary

enter image description here

  1. Create a new Project App MyLibraryDemo inside the package directory

enter image description here

  1. Open the project's .xcodeproj file

  2. Go to PROJECT > MyLibraryDemo > Add packages

enter image description here

  1. Press Add Local and select the directory containing your pacakge

enter image description here

  1. Go to the app's TARGET > General > Frameworks, Libraries and Embeded Content, and press 'add'

enter image description here

  1. Select your package from the list

enter image description here

  1. Now you can import your MyLibrary package into your example app, and edit / update your package from within you demo project.

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3