My location request view isn't working for some but I don't know where's my mistake?

My app has been refused for the second time because of the location request and for some time I didn't know why. Some nice people told me it was probably because I offer no other option than the location request to continue in the app. But fortunately, the App Store verification team confirmed to me it was simply because when they tap on the "Allow location" button nothing happens.

I don't see where my mistake is because on my side everything goes very well whether on a physical device or in the simulator.

Do you see anything? Could you test this code in Xcode and tell me what's going on for you?

// BootcampApp.swift

import SwiftUI

@main
struct BootcampApp: App {
    @StateObject private var locationManager = LocationManager()

    var body: some Scene {
        WindowGroup {
            ContentView()
                .environmentObject(locationManager)
        }
    }
}
// ContentView.swift

import SwiftUI

struct ContentView: View {
    @EnvironmentObject private var locationManager: LocationManager

    var body: some View {
        Group {
            if let userLocation = locationManager.userLocation, let cityName = locationManager.cityName {
                HomeView(userLocation: userLocation, cityName: cityName)
            } else {
                WelcomeView()
            }
        }
    }
}
// HomeView.swift

import SwiftUI
import CoreLocation

struct HomeView: View {
    let userLocation: CLLocation
    let cityName: String

    var body: some View {
        VStack {
            Text("Hello, World!")
        }
    }
}
// WelcomeView.swift

import SwiftUI

struct WelcomeView: View {
    @EnvironmentObject private var locationManager: LocationManager

    var body: some View {
        VStack {
            Button("Allow location", action: { locationManager.requestLocation() })
        }
    }
}
// LocationManager.swift

import Foundation
import CoreLocation

class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate {
    private let manager = CLLocationManager()
    @Published var userLocation: CLLocation?
    @Published var cityName: String?

    override init() {
        super.init()
        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBest
        manager.distanceFilter = 100
        manager.startUpdatingLocation()
    }

    func requestLocation() {
        manager.requestWhenInUseAuthorization()
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard let location = locations.last else { return }
        userLocation = location
        reverseGeocode(location)
    }

    func reverseGeocode(_ location: CLLocation) {
        let geocoder = CLGeocoder()

        geocoder.reverseGeocodeLocation(location) { placemarks, error in
            guard let placemark = placemarks?.first else { return }
            self.cityName = placemark.locality
        }
    }
}
// Info.plist

Privacy - Location When In Use Usage Description => "The app needs your location."

Thanks, in advance!

  • I actually discovered a silent error: "The operation couldn’t be completed. (kCLErrorDomain error 1.)".

Add a Comment