Wed, 2 Aug 2023 12:00:00 +0000

Swift Multi Platform Example Project

Helper Starter Project For Multi Platform Swift

TLDR; You can find a starter template project for examples on how to work with multi platform swift on the Swift Commons github org.

Todays post hilights a template project being hosted in the Swift Commons github org to provide a starter sample for those wanting to share swift across multiple platforms (apple or not).

You can check the project out here.

Walkthrough

The repo is in the form of one giant swift package. Although probably not the same approach everyone will take, I figured I'd test the limits while also adding some structure to this seemingly large format.

Plus, I'd be lying if I said I wasn't curious to see what kind of documentation would be generated from the root level of the package, as swift DocC traverses everything else ;)

In any case, inside the root Sources directory you'll find a folder labeled Examples. In there you'll find entire standalone projects that have multi platform swift elelments.

At the time of writing this post, there's only one example in there labeled MultiPlatformProjectExample. This is a monorepo style project that showcases a bunch of different platforms where swift is compiled and used.

This particular example has 2 main directories. a Libs dir which has a shared swift package used on every platform, and an Apps dir which contains various apps used for different platforms.

The idea is every app inside the Apps dir, uses the swift code in the Libs dir.

Apps Overview

The Apps directory constains directories for each target platform. - Darwin: has a multiplatform XCode project SwiftMultiPlatformDarwin that runs on iOS / iPad / macOS. - Desktop: has a multiplatform GtkApp that runs on Linux / Windows / macOS using GTK as its UI rendering library. - Web: has 2 different projects. 1 is a pure serverside VaporApp that is used to host on a web server and can return html and any api you need. The other is a WasmApp web assembly project that allows you to export web assembly binaries with swift. - Android: This directory is currently empty but will be updated with an Android project that incorporates a swift library in the near future. The project will be an android-studio project first with swift binaries linked with the NDK

More About The Apps

Each of these apps imports the same swift package from the above Libs folder. Making it possible to have shared swift logic on all these platforms while also leaning into a UI framework that fits the platform best.

On top of that, (with the exception of vapor) each of these platform apps use a SwiftUI type of framework to build out your UI, making it easy to leverage the speed and minimalist syntax when building out your apps on each platform.

The Darwin apps are pretty straightforward. Its an xcode project you should be able to build with any relatively new version of XCode (this one uses XCode 14.3)

The Desktop app should be able to run anywhere you have the GTK libraries installed and a Swift toolchain. You can get swift toolchains for Linux and Windows off of the official Swift website. On macOS you should be fine with just XCode. For more information on how to install GTK you can consult the GTK swift package github page

The Web apps should be pretty easy to run on Linux. Vapor lends it self to easily run just about wherever you have a swift toolchain. You can build a standalone app using just HTML and serve your app on web browsers easily.

The WASM app is pretty cool too if you want to build a web assembly binary that runs wherever web assembly is supported. Using the Tokamak framework you can compose web assembly UI's similar to how you do iOS apps on SwiftUI. There's even a static website version of th is you can use in Vapor or anywhere you serve HTML if you'd like to use it outside of WASM. I'll detail how I do this in later post.

The WASM toolchain requires its own swift web assembly toolchain to run and uses carton to bundle the files together.

Build Instructions

I took the liberty of making a github action set for this repo so users can follow it for instructions on how to build on their respective platforms.

Closing Thoughts

Hopefully this repo poves useful to anyone wondering how to go about approaching a cross platform swift project. As mentioned above, I'll be updating it periodically including with Android samples soon.

This is only the beginning for showing off how you can use Swift outside of Apple's platform. There are plenty of other ways you can use it like for game mods or general programming. I look forward to showing off all the ways I use Swift today.

Till next time