Creating the Package
To create a new SPM Package just follow this sequence:
It’s important to notice that your Package.swift must be placed
in the root of the repository.
This is only needed if the package will have it's own repository.
- Open Xcode
- Go to File → New → Package → Give it a name and hit Create.
That’s it. You have created your Package 🎉.
Creating the ExampleApp
To create the ExampleApp just follow this sequence:
- Open Xcode
- File → New → Project → App → Give it a name and hit Create.
That’s it. You have created your ExampleApp 🎉.
Now we can do some clean up to make the ExampleApp as simple as possible:
- Remove Main.storyboard
- Remove SceneDelegate.swift
- In the Info.plist, remove the following entries:
- Main storyboard file base name
- Application Scene Manifest
- Remove all the code in the AppDelegate and replace it with this:
import UIKit
@main
final class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
private lazy var navigationController = UINavigationController()
func application(
_: UIApplication,
didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
window = UIWindow()
guard let window = window else { fatalError("Can't start without a window") }
navigationController.setViewControllers([ViewController()], animated: true)
window.rootViewController = navigationController
window.makeKeyAndVisible()
return true
}
}
Linking the ExampleApp and the Package
In Xcode with the ExampleApp project open:
- File → Add packages… → Add Local → Select the root directory (The package) → Add Package.
- Now go to the Build Phases of the project → Link Binary with Libraries → Hit the
+
button → Select the package.
That’s it. Now the ExampleApp can import the Package 🎉.
Importing the Package on another project
This section assumes that your package is hosted on github.
Open your project in Xcode:
- File → Add packages… → Search for the github URL of the project → Add the dependency rules that you want → Add to your project → Add Package.
- Now you have to wait for Xcode to fetch the package.
That’s it. Now your project can import the Package 🎉.