Build iOS Packages with SPM: Step-by-Step
In this article
You build iOS-specific packages with Swift Package Manager by declaring an iOS platform requirement in Package.swift, keeping iOS-only code behind availability checks or in iOS-only targets, and then building or testing against an iOS destination with xcodebuild or Xcode. SPM itself is cross-platform; it does not compile for a device unless you tell it which platform and SDK to target. The shortest working set is: add platforms: [.iOS(.v16)] (or your minimum), guard iOS-only APIs with #if os(iOS) or @available(iOS ...), then run xcodebuild -scheme YourPackage -destination 'generic/platform=iOS' build. Labels and exact menu names can differ slightly by Xcode version, so treat the paths below as the current common layout.
What You'll Need
- A Mac with Xcode installed (SPM for Apple platforms ships with Xcode; you do not install it separately).
- Command Line Tools selected, so
swiftandxcodebuildresolve: Xcode > Settings > Locations > Command Line Tools. - A package directory containing a valid
Package.swiftmanifest. - For device builds: an Apple Developer account and a signing team configured in the target that consumes the package. A standalone package can build for a generic iOS destination without signing, but an app target that links it needs signing.
- Optional: an iOS Simulator runtime installed via Xcode > Settings > Components (or the Platforms tab, depending on version).
Step-by-Step
- Create or open the package. In Terminal, run
swift package init --type libraryinside an empty folder, or open an existing package folder in Xcode with File > Open. Xcode readsPackage.swiftand shows the package in the Project navigator. - Declare the iOS platform in the manifest. At the top level of
Package.swift, add aplatformsarray, for exampleplatforms: [.iOS(.v16), .macOS(.v13)]. Listing only iOS is allowed and makes the package iOS-only; listing macOS as well lets you run fast unit tests on the Mac. The version you pick becomes the minimum deployment target for consumers. - Separate iOS-only code. SPM compiles every target for every platform you declare, so iOS-only imports such as
UIKitmust be wrapped. Use#if os(iOS)around the file's contents, or mark individual declarations with@available(iOS 16, *). If a whole target is iOS-only, you can keep it out of the macOS build by not listing macOS inplatforms, or by giving the target a conditional dependency. - Check the manifest parses. Run
swift package describe. It prints targets, products and platforms without compiling. Errors here are manifest syntax errors, not code errors, and are much faster to fix. - Build for a generic iOS device. From the package folder run
xcodebuild -scheme YourPackageName -destination 'generic/platform=iOS' build. Useswift package describeorxcodebuild -listto confirm the scheme name; it usually matches the package or product name. This compiles against the iOS SDK without needing a connected device. - Build for the simulator. Run
xcodebuild -scheme YourPackageName -destination 'platform=iOS Simulator,name=iPhone 15' build, substituting a simulator that exists on your machine (list them withxcrun simctl list devices available). Simulator builds are the quickest way to catch iOS-only compile errors. - Run the tests on an iOS destination. Add a test target in
Package.swiftand runxcodebuild test -scheme YourPackageName -destination 'platform=iOS Simulator,name=iPhone 15'. Plainswift testruns on macOS only, so it will not exercise iOS-only code paths. - Consume the package from an app. In the app project choose File > Add Package Dependencies, enter the repository URL or select the local folder, and pick the iOS platform. Set the app's minimum deployment target to at least the package's declared iOS version, or resolution fails with a platform mismatch.
- Verify a release build. Repeat the
xcodebuildcommand with-configuration Releasebefore shipping. Debug-only code paths and assertion behaviour differ, and this catches optimisation-related compile errors.
Common Mistakes
- Forgetting the
platformsline. Without it SPM assumes very old minimums, and APIs you use may be rejected or the package may fail to resolve against a modern app. - Importing UIKit unconditionally. If macOS is listed in
platforms, the macOS build fails onimport UIKit. Guard it or drop macOS. - Mismatched deployment targets. An app with an iOS 15 minimum cannot link a package that requires iOS 17. The resolver reports this as an unsatisfiable platform requirement.
- Using
swift buildand expecting iOS output.swift buildtargets the host Mac. Usexcodebuildwith an iOS destination for iOS artefacts. - Wrong scheme name.
xcodebuildfails with "scheme not found" when the name does not match; check withxcodebuild -list. - Assuming resources are platform-filtered. Files in a target's resource bundle are included for every platform the target builds for; filter them in code if needed.
Tips
- Keep the iOS minimum as low as you genuinely support; raising it later is easy, lowering it after release is not.
- Put shared, platform-neutral logic in one target and iOS-specific UI or framework wrappers in another. This keeps the macOS test path usable and the iOS target small.
- Use
swift package describeandxcodebuild -listbefore long builds; they catch naming and manifest problems in seconds. - Pin dependency versions with
.upToNextMajor(from:)rather than branches so iOS builds stay reproducible. - Run the simulator test destination in CI alongside the macOS one; iOS-only regressions otherwise slip through.
Troubleshooting
If the build fails immediately with a manifest error, run swift package describe and fix the reported line; common causes are a missing comma in platforms or an unknown platform version.
If you see "no such module UIKit" on a macOS build, an iOS-only import is unguarded. Wrap the file in #if os(iOS) or remove macOS from platforms.
If resolution fails with a platform requirement error, compare the app's deployment target with the package's platforms entry and raise the lower one.
If xcodebuild cannot find the scheme, run xcodebuild -list in the package directory and copy the exact name. If no schemes appear, open the package in Xcode once so it generates them.
If a simulator destination is unavailable, list installed runtimes with xcrun simctl list runtimes and install one through Xcode's components settings, then retry with a device name that appears in xcrun simctl list devices available.
If a device build fails on signing, that is the consuming app's responsibility: open the app target, select your team under Signing & Capabilities, and rebuild. A standalone package build for generic/platform=iOS does not require signing.
Frequently Asked Questions
Can a Swift package target iOS only?
Yes. List only .iOS(...) in platforms. The package then resolves only for iOS consumers, and swift build on the Mac is not a meaningful check.
Does swift build compile for iOS?
No. It builds for the host platform, normally macOS. Use xcodebuild with an iOS or iOS Simulator destination for iOS output.
Do I need an Apple Developer account to build a package for iOS?
Not for a generic iOS build or simulator build. You need a team and signing only when an app target that links the package is built for a device or distributed.
How do I run package tests on iOS?
Use xcodebuild test with an iOS Simulator destination and a scheme that includes the test target. swift test runs on macOS only.
Why does adding the package to my app fail with a platform error?
The app's minimum iOS version is lower than the package's. Raise the app's deployment target to match or exceed the package requirement.
Can I ship iOS-only code in a multi-platform package?
Yes, if it is guarded with #if os(iOS) or availability attributes, or isolated in a target that only iOS builds include.