Examples
Basic tracking
import SwiftUI
import BackgroundGeolocation
@main
struct MyApp: App {
private var subscriptions = Set<BGGeo.EventSubscription>()
init() {
let bgGeo = BGGeo.shared
// Register event listeners *before* calling ready()
bgGeo.onLocation { event in
print("[onLocation] \(event.coords)")
}.store(in: &subscriptions)
// ready() configures the SDK and restores persisted state.
// It does NOT start tracking — call start()/stop() separately.
bgGeo.ready { config in
config.geolocation.desiredAccuracy = kCLLocationAccuracyBest
config.geolocation.distanceFilter = 10
config.app.stopOnTerminate = false
config.app.startOnBoot = true
config.logger.debug = true
config.logger.logLevel = .verbose
}
}
var body: some Scene {
WindowGroup { ContentView() }
}
}
// Elsewhere — e.g. a toggle-button action in your UI
struct TrackingButton: View {
@State private var isTracking = BGGeo.shared.state.enabled
var body: some View {
Button(isTracking ? "Stop" : "Start") {
Task {
let bgGeo = BGGeo.shared
if isTracking {
bgGeo.stop()
} else {
try await bgGeo.start()
}
isTracking = bgGeo.state.enabled
}
}
}
}