Please could you guys point out where i’m going wrong, i have cobbled this together as my first app on iOS and on the simulator it runs but gives no GPS data available, on an actual phone its just a black display.
After 4 hours of trying different things to no avail i’m stuck.
Not sure what other details to add.
import UIKit
import CoreLocation
import AVFoundation
class ViewController: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
let debugLabel = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
setupLocationManager()
startTimer()
}
func setupUI() {
// Set up debug label
debugLabel.frame = CGRect(x: 20, y: 100, width: view.frame.width - 40, height: 40)
debugLabel.textAlignment = .center
debugLabel.textColor = .black
debugLabel.text = "No GPS data available"
view.addSubview(debugLabel)
}
func setupLocationManager() {
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
// Check if location services are available
if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingHeading()
} else {
displayError(message: "Location services are not enabled. Please enable them in Settings.")
}
}
func startTimer() {
Timer.scheduledTimer(timeInterval: 1200, target: self, selector: #selector(triggerAnnouncement), userInfo: nil, repeats: true)
}
@objc func triggerAnnouncement() {
locationManager.startUpdatingHeading()
}
func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
let heading = newHeading.trueHeading
announceHeading(heading)
// Update debug label with GPS data
if let location = locationManager.location {
let latitude = location.coordinate.latitude
let longitude = location.coordinate.longitude
debugLabel.text = "Heading: \(heading) degrees\nLatitude: \(latitude)\nLongitude: \(longitude)"
}
}
func announceHeading(_ heading: CLLocationDirection) {
let synthesizer = AVSpeechSynthesizer()
let utterance = AVSpeechUtterance(string: "Current heading is \(heading) degrees")
synthesizer.speak(utterance)
}
func displayError(message: String) {
let alert = UIAlertController(title: "Error", message: message, preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(okAction)
present(alert, animated: true, completion: nil)
}
}




