Store Main storyboard in a Swift Package

Viewed 252

I'm playing around with Swift Packages in Xcode. I was wondering if it's possible to move your app's "main storyboard" into a Swift Package. By "main storyboard", I mean the one that's set in your info.plist under the key NSMainStoryboardFile (the human-readable name is Main storyboard file base name (macOS)

This is the folder structure of my package:

.
├── Package.swift
├── README.md
├── Sources
│   └── FooPackage
│       ├── FooPackage.swift
│       └── Resources
│           ├── Base.lproj
│           │   └── Main\ Menu.storyboard
│           └── img.png
└── Tests
    └── FooPackageTests
        └── FooPackageTests.swift

This is my package.swift file, unchanged from the Xcode template:

// swift-tools-version:5.5
// The swift-tools-version declares the minimum version of Swift required to build this package.

// swift-tools-version:5.3

import PackageDescription

let package = Package(
    name: "FooPackage",
    defaultLocalization: "en",
    products: [
        // Products define the executables and libraries a package produces, and make them visible to other packages.
        .library(
            name: "FooPackage",
            targets: ["FooPackage"]),
    ],
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages this package depends on.
        .target(
            name: "FooPackage",
            dependencies: []),
        .testTarget(
            name: "FooPackage",
            dependencies: ["FooPackage"]),
    ]
)

In a unit test, I check if my storyboard is accessible, and it is:

print(Bundle.module.path(forResource: "Main Menu", ofType: "storyboard")!)
// => /Users/Alex/FooPackage/.build/x86_64-apple-macosx/debug/FooPackage_FooPackage.bundle/Base.lproj/Main Menu.storyboard

As you see, I access it via Bundle.module. Other apps that include this Swift Package won't have access to that accessor. From what I can tell, this is an intended design: your package is expected to expose its "shared" bundle resources using explicitly defined public APIs.

Nonetheless, that doesn't appear compatible with the info.plist expectation that the main storyboard resides in the main bundle. Is there a way around this?

1 Answers

I've dealt with this before and I can't remember exactly, but I think you need to add this after dependencies in package

        resources: [
            .process("Main Menu")
        ]),

Keep in mind it won't be a default storyboard though. You'll have to call it from code.

Related