Please might you guys level out the place i am going fallacious, i’ve cobbled this collectively as my first app on iOS and on the simulator it runs however provides no GPS knowledge accessible, on an precise cellphone its only a black show.
After 4 hours of making an attempt various things to no avail i am caught.
Undecided what different particulars so as to add.
import UIKit
import CoreLocation
import AVFoundation
class ViewController: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
let debugLabel = UILabel()
override func viewDidLoad() {
tremendous.viewDidLoad()
setupUI()
setupLocationManager()
startTimer()
}
func setupUI() {
// Arrange debug label
debugLabel.body = CGRect(x: 20, y: 100, width: view.body.width - 40, top: 40)
debugLabel.textAlignment = .middle
debugLabel.textColor = .black
debugLabel.textual content = "No GPS knowledge accessible"
view.addSubview(debugLabel)
}
func setupLocationManager() {
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
// Examine if location providers can be found
if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingHeading()
} else {
displayError(message: "Location providers are usually not enabled. Please allow them in Settings.")
}
}
func startTimer() {
Timer.scheduledTimer(timeInterval: 1200, goal: self, selector: #selector(triggerAnnouncement), userInfo: nil, repeats: true)
}
@objc func triggerAnnouncement() {
locationManager.startUpdatingHeading()
}
func locationManager(_ supervisor: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
let heading = newHeading.trueHeading
announceHeading(heading)
// Replace debug label with GPS knowledge
if let location = locationManager.location {
let latitude = location.coordinate.latitude
let longitude = location.coordinate.longitude
debugLabel.textual content = "Heading: (heading) degreesnLatitude: (latitude)nLongitude: (longitude)"
}
}
func announceHeading(_ heading: CLLocationDirection) {
let synthesizer = AVSpeechSynthesizer()
let utterance = AVSpeechUtterance(string: "Present heading is (heading) levels")
synthesizer.converse(utterance)
}
func displayError(message: String) {
let alert = UIAlertController(title: "Error", message: message, preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", type: .default, handler: nil)
alert.addAction(okAction)
current(alert, animated: true, completion: nil)
}
}