I am trying to build a MVP with SwiftUI that simply shows me the changes in altitude on my Apple Watch. From there I will figure out where to go next (I want to use it for paragliding and other aviation things).
I have previous experience in python, but nothing in Swift, so even after a ton of tutorials I am very unsure about how and where to declare and then use functionalities.
Here is my code so far:
//
// ContentView.swift
// Altimeter WatchKit Extension
//
// Created by Luke Crouch on 29.09.20.
//
import SwiftUI
import CoreMotion
//class func isRelativeAltitudeAvailable() -> Bool
struct ContentView: View {
let motionManager = CMMotionManager()
let queue = OperationQueue()
let altimeter = CMAltimeter()
let altitude = 0
var relativeAltitude: NSNumber = 0
var body: some View {
if motionManager.isRelativeAltitudeAvailable() {
switch CMAltimeter.authorizationStatus() {
case .notDetermined: // Handle state before user prompt
fatalError("Awaiting user prompt...")
case .restricted: // Handle system-wide restriction
fatalError("Authorization restricted!")
case .denied: // Handle user denied state
fatalError("Auhtorization denied!")
case .authorized: // Ready to go!
print("Authorized!")
@unknown default:
fatalError("Unknown Authorization Status")
}
altimeter.startRelativeAltitudeUpdates(to: queue, withHandler: CMAltitudeHandler)
}
// something like relative Altitude = queue[..]
Text("\(relativeAltitude)")
.font(.largeTitle)
.fontWeight(.bold)
.foregroundColor(Color.green)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
I am getting multiple errors that I dont know how to deal with:
- Type 'Void' cannot conform to 'View', only struct/enum/class types can conform to protocols.
- Value of type CMMotionManager has no member 'isRelativeAltitudeAvailable'
- Type '()' cannot conform to View...
- Cannot convert value of type 'CMAltitudeHandler.Type' (aka '((Optional, Optional) -> ()).Type') to expected argument type 'CMAltitudeHandler' (aka '(Optional, Optional) -> ()')
Could you please give me some hints?
Thank you so much! Luke