How to Install MBProgressHUD with Swift Package Manager

How to Install MBProgressHUD with Swift Package Manager

Swift Package Manager is the modern way to add MBProgressHUD to your iOS app, and it is the method Apple recommends today. It is built into Xcode, so you do not need to install any extra tools, and you do not need to manage a Podfile or a Cartfile. You add the package, link the library to your app target, and you are ready to write code. This guide walks through the whole process, from opening your project to importing the library in your first view controller.

Before you start, make sure your project is ready. MBProgressHUD requires iOS 9.0 or later and ARC, which every modern Xcode project already uses. It is written in Objective-C, but it works in Swift projects through Swift Package Manager without a bridging header, because the package exposes it as a module. You need the GitHub repository address, which is github.com/jdg/MBProgressHUD.git, and you need an internet connection for Xcode to fetch the package the first time.

Open Your Project and Add the Package

Open your project in Xcode, then go to the File menu and choose Add Package Dependencies. A window will open with a search box at the top right. Paste the repository address into that box and press Enter. Xcode will contact GitHub and fetch the package information, which takes a few seconds. When it finds MBProgressHUD, you will see the package name and a short description. Click the package to select it.

Next, Xcode asks which version you want. The default choice is usually “Up to Next Major Version”, and that is a good choice for most projects. It means Xcode will use the latest stable version and will accept small updates, but it will not jump to a new major version without your approval. The latest stable version is 1.2.0, released in January 2020. Click Add Package to continue.

Link the Library to Your App Target

After you pick the version, Xcode shows a list of library products in the package. You will see MBProgressHUD listed there. Make sure your app target is selected in the “Add to Target” column, then click Add Package. Xcode will download the package, resolve the dependency, and add it to your project. You will see MBProgressHUD appear in the Project navigator under Package Dependencies.

That step is the one people sometimes miss. If you add the package but forget to link the library to your app target, your code will not compile, and the error will say the module cannot be found. If that happens, select your project in the navigator, go to your app target, open the “Frameworks, Libraries, and Embedded Content” section, and confirm MBProgressHUD is listed there. If it is missing, click the plus button and add it.

Import the Library in Your Code

Once the package is linked, you can import it in any Swift file with one line at the top: import MBProgressHUD. That line gives you access to the MBProgressHUD class and all of its methods. In Objective-C, you would import it with angle brackets, like this: #import <MBProgressHUD/MBProgressHUD.h>. Angle brackets mean the header comes from a framework or package, not from a file in your project.

Now try a quick test to confirm everything works. In a view controller, add a line that shows the HUD, such as MBProgressHUD.showAdded(to: self.view, animated: true). Build the project. If it compiles and the HUD appears when you run the app, the install worked. If the build fails with a “no such module” error, check the target link step above, then clean the build folder and try again.

Updating the Package Later

Swift Package Manager makes updates easy. When a new version of MBProgressHUD is released, you can update it from Xcode. Select your project, open the Package Dependencies tab, select MBProgressHUD, and click Update Package. Xcode will fetch the latest version that matches your version rule. Because the latest stable version is 1.2.0, and the library has been stable for years, you may never need to update it at all.

If you want to lock your project to an exact version, you can change the version rule when you add the package. Choose “Exact Version” and type 1.2.0. That guarantees every developer on your team uses the same code. For most small projects, the default rule is fine, but for a team project, pinning the version avoids surprises.

Common Problems and Quick Fixes

The most common problem is the “no such module” error. It usually means the library is not linked to your app target, as described above. The second most common problem is a version resolution failure, which happens when Xcode cannot reach GitHub. Check your internet connection, then try again. If Xcode shows a cached error, close the project, delete the derived data folder, and reopen the project.

Another problem is a deployment target mismatch. If your project targets an iOS version older than 9.0, the package will not build, because MBProgressHUD requires iOS 9.0 or later. Open your project settings, check the deployment target, and raise it to 9.0 or higher. Every modern app already targets a much newer version, so this problem is rare, but it is worth knowing about.

What Happens After You Add the Package

Once Xcode adds the package, it records the choice in a file called Package.resolved, which sits in your project folder. That file pins the exact version every developer on your team will use, so everyone builds with the same code. If you work alone, you can ignore the file. If you work on a team, commit it to version control along with the rest of your project, so a teammate who checks out the project gets the same MBProgressHUD version you use.

Xcode also stores the downloaded package in its derived data folder, so the next build is fast. If you ever switch machines, Xcode will download the package again the first time you open the project, which takes a few seconds. You do not need to copy the package files yourself, and you should not add them to version control. The Package.resolved file is enough for Xcode to fetch the right version on any machine.

Here is a short checklist for a clean Swift Package Manager install:

  • Confirm your project targets iOS 9.0 or later with ARC enabled
  • Confirm you added the package from github.com/jdg/MBProgressHUD.git
  • Confirm you linked the MBProgressHUD library product to your app target
  • Confirm you import the module with import MBProgressHUD in Swift
  • Confirm the project builds and the HUD appears when you run the app
  • Confirm you know how to update or pin the package version later

Conclusion

Installing MBProgressHUD with Swift Package Manager takes a few minutes. Open your project, choose File, then Add Package Dependencies, and paste the GitHub repository address. Pick a version rule, link the MBProgressHUD library to your app target, and import the module in your code. The latest stable version is 1.2.0, and it requires iOS 9.0 or later with ARC. If the build fails, check the target link first, then the deployment target, then your internet connection. With the package installed, you are ready to show your first loading indicator, and the next guides in this series show you exactly how to do that.

Read next: What Is MBProgressHUD?, How to Install MBProgressHUD with CocoaPods, MBProgressHUD in Swift: Bridging Header Guide

2 thoughts on “How to Install MBProgressHUD with Swift Package Manager”

  1. Pingback: Install MBProgressHUD with CocoaPods Guide

  2. Pingback: MBProgressHUD in Swift: Bridging Header Guide

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top