| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Hot reload on Swift app using @_dynamicReplacement
SwiftHotReload is an experimental project. We investigate a real world application of the @_dynamicReplacement feature of Swift 5.1+. Many portions are subject to change, including the library name (it's simple & naive name. we don't plan to publish to CocoaPods/Specs before resolving them.)
We can use either Standalone Reloader or Proxy Reloader. Standalone Reloader runs all required tasks on the runtime target process. Proxy Reloader runs on the runtime target process and receives dylibs from BuildHelper via network. BuildHelper runs on the host Mac and monitors file changes to build the file and send dylibs to Proxy on the target.
| Runtime Target App | Standalone | Proxy & BuildHelper |
|---|---|---|
| iOS app on Simulator | ✅ | ✅ |
| iOS app on Device | ❌ | ✅ (codesign with Individual, Company or Enterprise ADP) |
| macOS app (App Sandbox = NO) | ✅ | ✅ |
| macOS app (App Sandbox = YES) | ❌ | ❌ (codesign cannot be trusted to load) |
| macOS app (Designed for iPad) | ❌ | ❌ (codesign cannot be trusted to load) |
| visionOS app on Simulator | ✅ | ✅ |
| visionOS app on Device | ❌ | ✅ (codesign with Individual, Company or Enterprise ADP) |
SPM
https://github.com/banjun/SwiftHotReload.git
CocoaPods
pod 'SwiftHotReload', :git => "https://github.com/banjun/SwiftHotReload.git", :branch => "main"
or manual copy
Set up app as described below and build & run on a supported platform.
extension App {
static let reloader = StandaloneReloader(monitoredSwiftFile: URL(fileURLWithPath: #filePath).deletingLastPathComponent()
// file path to be monitored
.appendingPathComponent("RuntimeOverrides.swift")
:
_ = App.reloader // use to load the lazy static property above and start a file monitor
}If the app is for iOS Device, use ProxyReloader instead of StandaloneReloader. Run BuildHelper separately on the host Mac:
git clone https://github.com/banjun/SwiftHotReload.git cd SwiftHotReload swift run BuildHelper -c debug
Alternatively to swift run, we can run BuildHelper as an app (not CLI) using BuildHelper target on SwiftHotReload.xcworkspace.
Modify the app entitlements file:
App Sandbox = NO
or copy-and-paste on the Xcode build settings GUI to overwrite in 1 step.
OTHER_SWIFT_FLAGS[config=Debug] = -Xfrontend -enable-implicit-dynamic -Xfrontend -enable-private-imports
@ObservedObject private var reloader = App.reloaderAny funcs/vars can be replaced (not only for SwiftUI).
import AppModuleName
extension ContentView { // <- typically use extension for a type containing func/var to be replaced
@_dynamicReplacement(for: body) // <- func/var name to be replaced
var body2: some View { // <- use different name than the original
:
}
} | Back | FazBrowse Home | New Git URL |