Prompt User to Choose Location Preference Not Working

At the moment with my app Users have to go to Privacy & Security -> Locations Services -> On -> Scroll down to my app and turn on their Location Preference. I am trying to incorporate in to my app the Code below to make it unecessary to leave the app to enable the User‘s location to be displayed but it does not work. Please tell me, what am I missing here?

import MapKit
import SwiftUI
import CoreLocation
// Prompt User to choose Location Preference //
class ViewController: UIViewController, CLLocationManagerDelegate{
    var locationManager: CLLocationManager?
    override func viewDidLoad() {
        super.viewDidLoad()
        locationManager = CLLocationManager()
        locationManager?.delegate = self
        locationManager?.requestWhenInUseAuthorization()
        view.backgroundColor = .gray
    }
}
// The rest of the Code which is working okay ( Map etc.) //
struct Positions: Identifiable {
    let id = UUID()
    let name: String
    let latitude: Double
    let longitude: Double
    var coordinate: CLLocationCoordinate2D {
        CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
    }
}
etc.
etc.
  • Further investigated the above and now understand maybe the Code is incomplete? Regarding the second version below which I prefer, I don’t yet see why it doesn’t work.

Add a Comment

Accepted Reply

Found this solved my problem and is very informative to boot!

[https://youtu.be/poSmKJ_spts?si=z3r3gkDuQtkXtVmb)

Replies

Also tried this without success.

class RequestLocationManager: NSObject, CLLocationManagerDelegate {
    private let locationManager = CLLocationManager()
    override init() {
        super.init()
        locationManager.delegate = self
    }
    func requestUserAuthorization() async throws {
        locationManager.requestWhenInUseAuthorization()
    }
}

Have done the neccessary Permission.

Also tested it outside of Playgrounds with TestFlight but no success.

Found this solved my problem and is very informative to boot!

[https://youtu.be/poSmKJ_spts?si=z3r3gkDuQtkXtVmb)