The way to Share SwiftUI Code Utilizing Swift Packages


Within the earlier tutorial, we created an animated menu bar in SwiftUI. What if you wish to reuse the code in different tasks? In fact, you may copy & paste the code from one challenge to a different however there’s a higher method to reuse the code through the use of Swift Bundle.

Swift packages are reusable parts that builders can import of their tasks. With Swift Bundle Supervisor, the built-in software for creating and managing Swift packages, you may simply share reusable code within the type of Swift packages.

On this tutorial, I’ll stroll you thru the method of making Swift packages and present you the way to flip the code of the animated menu bar right into a reusable SwiftUI part.

Please be aware that I take advantage of Xcode 13 to create the demo code. Nevertheless, you must be capable to comply with procedures even if you’re utilizing a decrease model of Xcode.

Creating Swift Packages

There are two methods to create a Swift package deal. You possibly can both use command line or Xcode.

Utilizing Command Line

To create a Swift package deal utilizing command line, open Terminal and key within the following instructions:

The folder title is the package deal title. Right here, we use the title AnimatedMenuBar. When you hit the return key, you will notice the next messages:

This generates the essential skeleton of the Swift package deal together with the supply and exams. You possibly can additional edit file to supply an outline of the package deal. Bundle.swift is the manifest file which defines the package deal’s title and its contents utilizing the PackageDescription module.

Word that the package deal manifest should start with the string // swift-tools-version:, adopted by a model quantity resembling // swift-tools-version:5.3.

Utilizing Xcode

For those who favor to make use of Xcode to create the package deal, you may select File > New > Venture… and choose Swift Bundle below Multiplatform.

Updating the Supply

The AnimatedMenuBar.swift file below the Sources folder solely accommodates the default content material generated by Xcode:

It’s important to replace the file with the code for creating the animated menu bar. On this case, we reuse the code that we have now walked you thru within the earlier tutorial.

For Swift package deal, the AnimatedMenuBar struct is required to set to public. And we have to create a customized init with public entry degree.

Besides that, the remainder of the code is nearly the identical. Chances are you’ll discover one other distinction that we use the @obtainable attribute to annotate the struct with availability data. The road of code signifies that the struct is barely obtainable for iOS 14 and macOS 11.0 (or later).

Enhancing Check Code

By default, Xcode generates a take a look at folder so that you can embrace automated exams. You possibly can modify the generated file named AnimatedMenuBarTests.swift to incorporate your take a look at code. Nevertheless, for this demo, we’re not going to put in writing the code. You possibly can simply remark out the next line of code:

Including Dependencies (Non-obligatory)

Although this package deal doesn’t rely upon different Swift packages, you may edit the dependencies part to incorporate your dependent packages if you happen to want:

Including Supported Platforms

Whereas Swift packages are supposed to supply multiplatform help, you should utilize the platforms attribute in Bundle.swift if the package deal solely helps a sure platform. Right here is an instance:

For this demo package deal, it’s obtainable for iOS 14 and macOS 11.0 (or later).

Publishing the Bundle on GitHub

After you made all of the adjustments, you must be capable to construct your package deal to make use of it regionally. To additional share the package deal with builders in your staff or neighborhood, you may publish the package deal on GitHub.

Go as much as the Xcode menu and select Supply Management > New Git Repositories… to create a brand new respository.


Subsequent, swap over to the Supply Management Navigator. Proper click on Remotes and select New “AnimatedMenuBar ” Distant


Assuming you’ve already configured your GitHub account in Xcode, you must be capable to create a distant repository. Set the repository title to AnimatedMenuBar and key in your description of the package deal. Relying in your desire, you may make the package deal obtainable to public or simply maintain it to your individual challenge. For this demo, I set it to public.


When you hit the Create button, Xcode will create the repository on GitHub and add the native information to the repository.

At the moment, the package deal will not be assigned with a model quantity. To set a model for the package deal, go to the Supply Management Navigator. Proper click on the entry of the preliminary commit and select Tag.


Subsequent, set the tag to 1.0.0 and click on Create to substantiate the change.


The change you simply made is barely obtainable regionally. To set the tag on the distant repository, it’s essential to push the adjustments. Go as much as the Xcode menu, select Supply Management > Push. Please be sure you tick the Embody tags checkbox earlier than hitting the Push button.


That’s it! You’ve efficiently printed the Swift package deal onto GitHub. And, it’s accessible by way of

Utilizing Swift Bundle

To make use of the Swift package deal in any Xcode challenge, select File > Add Bundle… and key within the package deal URL within the search bar.


Xcode ought to then present you the package deal description and model. Click on Add Bundle to obtain and add the package deal to your challenge.


As soon as the package deal is downloaded, you must see the package deal below Bundle Dependencies within the challenge navigator. Now you might be prepared to make use of the AnimatedMenuBar view within the challenge.

All it’s essential to do is import the AnimatedMenuBar package deal and use the AnimatedMenuBar view like this:


On this tutorial, I’ve walked you thru the steps to create a Swift package deal for reusing some widespread SwiftUI views. The approach shouldn’t be restricted to the reuse of SwiftUI views. You possibly can apply it to widespread parts that may be shared between groups and tasks.

What do you concentrate on Swift packages? Have you ever used Swift Bundle Supervisor to create shareable parts? Please depart me remark and let me know.