Skip to main content

Develop Swift iOS Apps on Windows: SwiftUI, SPM, Debugging

Open an Xcode project and build, with SPM dependencies resolved automatically. Pure Swift packages can be converted to an app to produce an .ipa.

Last updated

Create a SwiftUI project in VS Code on Windows, debug it with breakpoints on an iPhone, and build a Release .ipa.
Show steps for

To develop Swift iOS apps on Windows, open your existing Xcode project in KXBuild: it builds Swift and SwiftUI, installs the app on your iPhone over USB for breakpoint debugging, and a Release build produces an .ipa. Build settings, target dependencies and compiler flags are read by Xcode's rules, so the project works as is, with Swift Package Manager and mixed Objective-C supported. Swift projects come in two kinds, Xcode projects and standalone Swift packages with only a Package.swift. For CocoaPods, see use CocoaPods on Windows.

How do I create or open a Swift iOS project on Windows?

A project with an .xcodeproj or .xcworkspace. Dependencies can be Swift packages, which are resolved automatically before each build.

1. Create a project

If you don't have a project yet, create one.

Run KXBuild: New Project from the Command Palette and pick the "iOS App (SwiftUI)" or "iOS App (Swift, UIKit)" template.

To use an existing project, open its folder in VS Code with File > Open Folder…, or drag the folder into VS Code.

> kxbuild project create -t app_swiftui -n MyApp -o D:\projects

2. Run doctor

Doctor checks your build environment and fixes anything missing. If the project has .metal shaders, first install Apple's Metal Developer Tools for Windows once by hand (the download requires an Apple Account sign-in); it is picked up automatically.

In the title bar of the KXBuild view, open the ··· menu and run Doctor (fix environment).

> kxbuild doctor .\MyApp.xcodeproj

How do I add a Swift package dependency to an Xcode project?

3. Add a Swift package dependency

This is the equivalent of Add Package Dependencies in Xcode. As in Xcode, a remote package defaults to its latest version and allows updates up to the next major version, and adding one needs a network connection.

Run KXBuild: Add Swift Package Dependency… from the Command Palette, or right-click a target in the KXBuild view and choose it there. Enter the package's Git URL or pick a local package folder, then choose the products you need.

Run the command for where the package lives.

Remote package

> kxbuild project add-package -P .\MyApp.xcodeproj --url https://github.com/Alamofire/Alamofire.git --product Alamofire

Local package

> kxbuild project add-package -P .\MyApp.xcodeproj --path ..\MyKit

To set a version or target, see project add-package.

How do I debug Swift and SwiftUI code on an iPhone?

4. Write code

Open a .swift file and you get completion, go to definition and error checking, including for SwiftUI and system framework APIs. Build once before you import modules from other targets or Swift packages in the project. If completion misbehaves, see Code completion and indexing.

5. Debug

Connect your iPhone over USB.

Select it in the KXBuild view and press F5. KXBuild builds, installs and stops at your breakpoints. Breakpoints work in both Swift and Objective-C code.

Run the command below to build the app, install it on your iPhone and launch it.

> kxbuild run .\MyApp.xcodeproj

Before you connect a phone for the first time, install and open Apple Devices from the Microsoft Store to get the USB driver on Windows. On iOS 16 and later, turn on Developer Mode in Settings > Privacy & Security, and register the phone's UDID in the provisioning profile you debug with. The first time you debug on a given iOS version, its symbols are downloaded automatically. They take a few GB and download only once. See connect an iPhone to Windows.

How do I build a Swift app as an .ipa for the App Store?

6. Release

Build with the Release configuration to get build\MyApp.ipa in the project folder. To upload it to App Store Connect, see publish to the App Store from Windows.

Run KXBuild: Build for App Store (.ipa) from the Command Palette or the project's context menu.

Build the .xcworkspace if there is one, otherwise the .xcodeproj, for example:

> kxbuild build .\MyApp.xcodeproj -c Release --export app-store

How do I put a standalone Swift package on an iPhone?

For a project with only a Package.swift and no Xcode project, open the folder containing Package.swift in VS Code to write and compile-check code. A Swift package can't be packaged as an app on its own, and that's true on a Mac too. To install it on an iPhone or produce an .ipa, find your case below. Swift Playgrounds .swiftpm packages that use AppleProductTypes don't build; turn them into a regular Package.swift first.

  • The package is an app. Its executable target holds a SwiftUI or UIKit app entry point. Follow the steps below to convert it to an app project.
  • The package only provides libraries for apps to use. In the app project, add it as a Swift package dependency as in step 3 above.

1. Write code and compile-check

Open a .swift file and you get completion, go to definition and error checking, including for SwiftUI, UIKit and other iOS framework APIs. To compile-check, run this in the folder containing Package.swift.

> kxbuild build .

2. Convert to an app project

Converting adds a MyApp subfolder to the package folder with the app project inside. The app still uses the package's code, so when you edit the package, you edit the app. Commit this subfolder to version control along with the package.

If the app uses a target from the same package that has no matching library product, the conversion adds a .library entry to products in Package.swift. After converting, review and commit that change to Package.swift.

Run KXBuild: Convert Swift Package to App Project from the Command Palette (or click the package's row in the KXBuild view), then enter the app name and bundle ID. The new project becomes the startup project.

Run this in the folder containing Package.swift.

> kxbuild project spm2app . -n MyApp -b com.example.myapp

3. Debug and release

In the KXBuild view, switch the startup project to MyApp.

Follow the debug and release steps for Xcode projects above. The .ipa is written to MyApp\build\MyApp.ipa.

On the command line, build this project, for example:

> kxbuild build .\MyApp\MyApp.xcodeproj -c Release --export app-store

How do I fix Swift build errors?

SymptomFix
A Debug build fails to link with undefined symbol: …vpfiA known issue in the open-source Swift compiler. It happens with a property that has an initial value, such as @State private var count = 0, when the struct's init is in another file. Set the environment variable KXBUILD_SWIFT_WHOLE_MODULE=all, reopen VS Code and build again. Swift targets are then built as a whole module. See Troubleshooting.

When a build fails, run doctor once, then look up the error message in troubleshooting build errors. Sign with your own .p12 certificate and .mobileprovision profile in a keychain/ folder and every build signs automatically; see sign iOS apps on Windows. With this flow you develop Swift iOS apps on Windows end to end: code, on-device debugging and .ipa builds all happen on your own computer.

Next steps

Don't see your framework?

kxbuild is a general-purpose build toolchain, so if your framework produces an Xcode project, KXBuild can build it. The list above covers common cases only. Tell us about your setup.