0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

iOS15 LocationButton

Last updated at Posted at 2021-07-26

#エラー発生

  • 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()
        }

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?