16

I am developing a package of Swift Package Manager where SwiftUI views need to be included.

Every SwiftUI view should have a preview.

However, when I try to run a preview of any view I get the following error: enter image description here

I found a way where if I change the package scheme to the main target scheme it becomes work. But using this way I have to switch between schemes all the time and the main target needs to be built every time I want to run a preview of a package.

Is it possible to run SwiftUI preview somehow directly from SPM Package without building the main target?

3 Answers 3

6

Xcode 12

With Xcode 12 the SwiftUI Preview just works 'as-should' in standalone Package

demo

Xcode 11+

Is it possible to run SwiftUI preview somehow directly from SPM Package without building the main target?

No, at least till now (Xcode 11.4beta3). Preview is a variant of Simulator and it needs UI executable to setup full-functional run-time context for your view preview.

Solution (from practice): setup SwiftUI executable target that in parallel contains all files from package (or package itself, depends) and perform all SwiftUI development it it, but package itself build during continuous integration process (including unit-testing).

Sign up to request clarification or add additional context in comments.

1 Comment

And make sure you don't specify your Library as static, in Package.swift. Leave the default or specify it as dynamic
3

What you need is to add in your swiftpackage declaration a platform target such as iOS see example below (with iOS(.v13)):

let package = Package(
    name: "NAME",
    platforms: [.iOS(.v13)],
    products: [
        // Products 
    ],
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
    ],
    targets: [
        // Targets
        // Test Targets
    ]
)

And now, make sure you use the same target for the preview. You can add more of course.

enter image description here

2 Comments

if anybody like me was feeling dumb for a while, you need to add a PreviewProvider to the file or the ui won't appear :)
also, if using a new package, delete the auto-generated xctescase. (you can add a new test case and use normally) developer.apple.com/forums/thread/651547
0

In Xcode 12 that has changed. Previews are enabled in Swift Packages. In fact, a lot of changes and new features were added that improve the user and developer experience for SwiftUI library extensions. You can watch this WWDC20 session to learn more.

To get it to work in the first beta of Xcode 12 you have to create a library Swift Package. Make sure you program can build - and that SwiftUI is imported. Also, remember to add the platforms supported in the package manifest - that are supported by SwiftUI and your package code. In the scheme selector select the library that you want to preview and a device supported by your library. After that enable the canvas the preview should be visible. There are currently some rough corners, but I hope errors and usability will improve on later versions.

3 Comments

Any additional help getting this to work? I have: 1. Using Xcode 12 create a new swift package 2. Create a simple view with Text("Hello World") 3. Tried to run the preview It does not work and comes up with errors. What steps are needed to get a vanilla swift package with 1 view to support previews?
I’ve updated the post with instructions. If you have any further questions, I recommend posting a new question for discoverability.
It doesn't work out of the box quite like the video. Package.swift needs to include the platforms, and if the dummy package file is included as is then the project compiles but the preview will say it didn't until you either rename the struct in that file or delete the file. Soon after that it will then start saying it couldn't complete in a reasonable amount of time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.