Contents

ezefranca/speedmanagermodule

> Simple Speedometer class to iOS and WatchOS.

Installation

The Swift Package Manager is the easiest way to install and manage SpeedManagerModule as a dependency. Simply add SpeedManagerModule to your dependencies in your Package.swift file:

dependencies: [
    .package(url: "https://github.com/ezefranca/SpeedManagerModule.git")
]

Update Info.plist

Add the correct permission descriptions

    <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
    <string>Your description why you should use NSLocationAlwaysAndWhenInUseUsageDescription</string>
    <key>NSLocationAlwaysUsageDescription</key>
    <string>Your description why you should use NSLocationAlwaysAndWhenInUseUsageDescription</string>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>Your description why you should use NSLocationAlwaysAndWhenInUseUsageDescription</string>

Add the background location updates in xcode

Or add the info to the Info.plist

    <key>UIBackgroundModes</key>
    <array>
        <string>location</string>
    </array>

Usage example

@StateObject

import SwiftUI

struct ContentView: View {
    
    @StateObject var speedManager = SpeedManager(.kilometersPerHour)
    
    var body: some View {
        VStack {
            switch speedManager.authorizationStatus {
            case .authorized:
                Text("Your current speed is:")
                Text("\(speedManager.speed)")
            default:
                Spacer()
            }
        }
    }
}

Using Delegates

import UIKit

class SpeedViewController: UIViewController {

    var speedManager = SpeedManager(.kilometersPerHour)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.speedManager.delegate = self
        self.speedManager.startUpdatingSpeed()
    }
}

extension SpeedViewController: SpeedManagerDelegate {
    
    func speedManager(_ manager: SpeedManager, didUpdateSpeed speed: Double, speedAccuracy: Double) {
        // Update UI with the current speed and accuracy
    }
    
    func speedManager(_ manager: SpeedManager, didFailWithError error: Error) {
        // Handle error
    }
    
    func speedManager(_ speedManager: SpeedManager, didUpdateAuthorizationStatus status: SpeedManagerAuthorizationStatus) {
        // Handle authorization status update
    }
    
    func speedManagerDidFailWithLocationServicesUnavailable(_ speedManager: SpeedManager) {
        // Handle location services unavailable
    }
}

Changing Unit

Just choose the unit during the class init.

    var speedManagerKmh = SpeedManager(.kilometersPerHour)
    var speedManagerMs = SpeedManager(.metersPerSecond)
    var speedManagerMph = SpeedManager(.milesPerHour)

Demo

Check the Demo folder to see it in action.

Meta

@ezefranca – @ezefranca

Distributed under the MIT license. See LICENSE for more information.

https://github.com/ezefranca/SpeedManagerModule

[swift-image]:https://img.shields.io/badge/swift-5.0-orange.svg [swift-url]: https://swift.org/ [license-image]: https://img.shields.io/badge/License-MIT-blue.svg [license-url]: https://github.com/git/git-scm.com/blob/main/MIT-LICENSE.txt

Package Metadata

Repository: ezefranca/speedmanagermodule

Default branch: main

README: README.md