Post

Replies

Boosts

Views

Activity

Location Retrieval in watchOS for Alarm App
Hello! I'm currently working on a panic alarm app and am in the process of developing an extension for the Apple Watch. I'm trying to retrieve the location on watchOS, but I've encountered some issues. Could you provide an example or guide on how to successfully obtain the location? I suspect there might be a problem with the didFailWithError method not being recognized. Any assistance would be greatly appreciated. // Home2.swift // (watchOS) Watch App // // Created by Admin on 15.08.2023. // import SwiftUI import CoreLocation import CoreLocationUI class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate { let manager = CLLocationManager() @Published var location: CLLocationCoordinate2D? override init() { super.init() manager.delegate = self } func requestLocation() { manager.requestLocation() } func checkPermission() { print("authorization status:") switch manager.authorizationStatus { case .authorizedAlways: print("always") break case .authorizedWhenInUse: print("authorized when in use") break case .notDetermined: print("notdetermined") break case .denied: print("denied") break case .restricted: print("restricted") break default: print("none of the above") break } } func requestPermission() { checkPermission() manager.desiredAccuracy = kCLLocationAccuracyBest manager.requestWhenInUseAuthorization() } public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { location = locations.first?.coordinate } public func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { // Error handling print("Error requesting location") } } struct Home2: View { @Binding var alarmStatus: String @StateObject var locationManager = LocationManager() @State var error: String = "" func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { // handle the error print("error ting location fra struct:" + error.message!) } func callForHelpSync() { let name = "EnBruger" /*do { try locationManager.requestPermission() print("her") let locVal = try locationManager.requestLocation() print("locVal: \(locVal)") } catch { print("locVal error: \(error)") } */ let userId = ReadlocalStorage(key: "userId") let lat: Double = 123 // TODO let long: Double = 1234 // TODO Task { let res = try await callForHelp(name: name, id: userId, lat: lat, long: long) print("callforhelp res: " + res as Any) if (res == "Sent") { //opdater binded value. alarmStatus = "Sent" } else { // vis error besked. error = "Error: \(res)" } } } var body: some View { VStack(){ Image("logo") .resizable() .aspectRatio(contentMode: .fit) Spacer() Button(action: { print("record clicked") WKInterfaceDevice.current().play(.click) }) { VStack() { Text("Aktiver Alarm") .multilineTextAlignment(.center) .padding(1.0) .font(.system(size: 22, weight: .bold)) Text("(Hold inde)") .multilineTextAlignment(.center) .padding(1.0) } } .padding(11.0) .background(/*@START_MENU_TOKEN@*//*@PLACEHOLDER=View@*/Color(hue: 1.0, saturation: 0.966, brightness: 0.824)/*@END_MENU_TOKEN@*/) .buttonStyle(.plain) .simultaneousGesture(LongPressGesture(minimumDuration: 2).onEnded({_ in callForHelpSync()})) Text(self.error).font(.footnote).foregroundColor(Color.red).fixedSize(horizontal: false, vertical: true) Spacer() Button(action: { print("test button 1 clicked") locationManager.requestPermission() }) { VStack() { Text("Test Knap 1") .multilineTextAlignment(.center) .padding(1.0) .font(.system(size: 22, weight: .bold)) } } .padding(6.0) .background(.blue) .buttonStyle(.plain) if let location = locationManager.location { Text("Your location: \(location.latitude), \(location.longitude)") } LocationButton { locationManager.requestLocation() } .frame(height: 44) .padding() Button(action: { print("test button 2 clicked") var lh = LocationHandler() lh.requestLocation() }) { VStack() { Text("Test Knap 2") .multilineTextAlignment(.center) .padding(1.0) .font(.system(size: 22, weight: .bold)) } } .padding(6.0) .background(.blue) .buttonStyle(.plain) } .background(/*@START_MENU_TOKEN@*//*@PLACEHOLDER=View@*/Color.white/*@END_MENU_TOKEN@*/) } } struct Home2_Previews: PreviewProvider { static var previews: some View { @State var alarmStatus = "none" Home2(alarmStatus: $alarmStatus) } }
0
1
391
Oct ’23