Please may you guys level out the place i am going unsuitable, i’ve cobbled this collectively as my first app on iOS and on the simulator it runs however offers no GPS knowledge obtainable, on an precise telephone its only a black show.
After 4 hours of attempting 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, peak: 40)
debugLabel.textAlignment = .middle
debugLabel.textColor = .black
debugLabel.textual content = "No GPS knowledge obtainable"
view.addSubview(debugLabel)
}
func setupLocationManager() {
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
// Verify if location companies can be found
if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingHeading()
} else {
displayError(message: "Location companies usually are 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)
}
}