iOS15 LocationButton


エラー発生

  • iOS15から導入された「LocationButton」普通に書くと [Error Domain=kCLErrorDomain Code=1 "(null)] が出る 結局、「requestLocationAuth()」とPlistへのお約束が必要だった(XCode13にはデフォルトないけど) 確認するには「stopLocation()」で一回update Locationを止めて確認する
LocationButton
class LocationButtonViewModel: NSObject, ObservableObject, CLLocationManagerDelegate {
//その他省略
    func requestLocation() {
        locationManager.requestLocation()
    }
    func stopLocation() {
        locationManager.stopUpdatingLocation()
    }

     func requestLocationAuth(man: CLLocationManager) {

        locationManager.requestAlwaysAuthorization()
        locationManager.desiredAccuracy = kCLLocationAccuracyReduced

        switch man.authorizationStatus {
        case .authorizedAlways:
            locationStatus = "authorized always"
            checkLocationAccuracyAllowed()
        case .authorizedWhenInUse:
            locationStatus = "authorized when in use"
            checkLocationAccuracyAllowed()
        case .notDetermined:
            locationStatus = "not determined"
        case .restricted:
            locationStatus = "restricted"
        case .denied:
            locationStatus = "denied"
        default:
            locationStatus = "other"
        }

    }
}
struct ContentView: View {
    @ObservedObject var model = LocationButtonViewModel()

        var body: some View {

        LocationButton(.currentLocation) {
            locationVM.requestLocation()
        }