Add a Swift package to your macOS app¶
You can add Swift Package Manager (SPM) dependencies to your macOS app under
[tool.kivy.macos.native.swift_packages]: an SDK such as Firebase, or a shim
of @objc classes that your Python code calls. kivyforge pins each remote
package in the lock, builds every package into a framework with xcodebuild,
and puts the framework in the .app. Your Python code loads it and calls its
classes through Rubicon-ObjC.
The keys are the iOS ones, but the result is not: a macOS .app has no Xcode
project, so nothing links the packages into your app's executable. Python
loads them when your code asks.
Before you begin¶
- Build a macOS .app without packages first.
- Install the full Xcode and select it:
sudo xcode-select --switch /Applications/Xcode.app. The command-line tools haveswiftbut notxcodebuild, which the build needs. - If you write a shim, read Call Swift from Python. Its rules
for
@objcclasses and its Rubicon-ObjC examples apply on macOS unchanged.
Declare the packages¶
-
Add the packages to your macOS overlay:
[tool.kivy.macos.native.swift_packages] SwiftBridge = { path = "swift-shims", products = ["SwiftBridge"] } Promises = { url = "https://github.com/google/promises", requirement = { from = "2.4.0" }, products = ["Promises"] }url,path,requirementandproductsmean what they mean on iOS; see the reference. A local package'spathis relative to the project root.linkandembedare refused: every product goes into the same framework. -
If a local package declares only
platforms: [.iOS(...)], it still builds for macOS at SwiftPM's default minimum. If it uses an API newer than that, add a macOS platform to itsPackage.swift, for example.macOS(.v13), and setminimum_system_versionin[tool.kivy.macos]to at least that version. -
Add Rubicon-ObjC to your dependencies and lock:
[project] dependencies = ["rubicon-objc>=0.5.7"]kivyforge lock -p macosThe lock runs
swift package resolvefor remote packages and records each one's revision inpylock.macos.toml. Local packages are recorded by path and are not resolved.
Build¶
kivyforge build -p macos
The build writes a root package in build/macos/spm/ that depends on every
locked package, and builds its one dynamic library, KivyforgePackages, with
xcodebuild. The first build clones remote packages; later builds reuse the
checkouts and compiled code in build/macos/spm/.
The .app gets:
| What | Where |
|---|---|
KivyforgePackages.framework, which links every product that leaves its type unset |
Contents/Frameworks/ |
A framework for each product its package declares type: .dynamic, such as a shim's |
Contents/Frameworks/ |
Each package's resource bundle, <package>_<target>.bundle |
Contents/Resources/ |
They are signed with the rest of the bundle, ad hoc or with your Developer ID.
Bundle.module, which package code uses to find its resources, looks in
Contents/Resources/ first.
Load the framework from Python¶
Load KivyforgePackages once, before you look up a class. Loading it loads the
other frameworks too:
import sys
from pathlib import Path
from rubicon.objc import ObjCClass
from rubicon.objc.runtime import load_library
frameworks = Path(sys.prefix).parents[1] / "Frameworks"
load_library(str(frameworks / "KivyforgePackages.framework" / "KivyforgePackages"))
notebook = ObjCClass("SBNotebook").alloc().initWithTitle("Groceries")
sys.prefix is the bundled runtime, Contents/Resources/python, so two
levels up is Contents/. Nothing is loaded at launch: until this call, the
Objective-C runtime has none of the packages' classes.
Verify¶
- Run
kivyforge doctor -p macos. With packages declared, it checks forswift(to lock), forxcodebuild(to build), and that each local package has aPackage.swift. - Build, then run the app and call one of your classes. The
swift-bridgeshim works as it is: a run on macOS constructedSBNotebook, polled anSBWorkjob to completion, and found a remote package's resource bundle in the main bundle. -
Check the signature covers the frameworks:
codesign --verify --deep --strict "build/macos/My App.app"
Things to know¶
- Remote packages are pinned, their dependencies are not. The lock records the revision of each package you declare. SPM resolves the packages they depend on when the build runs, as on iOS.
- Products that force
.dynamicstay separate frameworks. That is fine: each finds the others inContents/Frameworks/by itself. -ObjCis always on, so a static library inside a package keeps its Objective-C classes and categories even when nothing in Swift refers to them.- A build error fails with
KF-BUILD-TOOL-FAILEDand showsxcodebuild'serror:lines. The generated package is inbuild/macos/spm/Package.swift;xcodebuildrun there reproduces it.
What's next¶
- Call Swift from Python to write a shim.
- Sign, notarize, and staple a macOS app.