Post not yet marked as solved
Post marked as unsolved with 0 replies, 144 views
Hi! I'm new to Swift and I'm trying to build a simple app with a feature that will allow a user to send an sms that automatically includes their location. I've got the sending of the message covered however I'm lost at how I can automatically put the user's current location in the message's body. Any kind of help would be much appreciated, thank you!
Here is my code so far:
import SwiftUI
import CoreLocation
import UIKit
struct MessageView: View {
@State private var isShowingMessages = false
var body: some View {
Button("Show Messages") {
self.isShowingMessages = true
}
.sheet(isPresented: self.$isShowingMessages) {
MessageComposeView(recipients: ["09389216875"], body: "Emergency, I am here with latitude: \(locationManager.location.coordinate.latitude); longitude: \(locationManager.location.coordinate.longitude) {messageSent in
print("MessageComposeView with message sent? \(messageSent)") \\ I currently get an error in this chunk
}
}
}
class ViewController: UIViewController, CLLocationManagerDelegate {
var locationManager: CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status != .authorizedWhenInUse {return}
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
let locValue: CLLocationCoordinate2D = manager.location!.coordinate
print("locations = \(locValue.latitude) \(locValue.longitude)")
}
}