LocationManager Error when requesting location

Hi . I am following the simple example from hacking with swift website. It is to request a location.

import CoreLocation
import CoreLocationUI

class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate {
  let manager = CLLocationManager()

  @Published var location: CLLocationCoordinate2D?

  override init() {
    super.init()
    manager.delegate = self
  }

  func requestLocation() {
    manager.requestLocation()
  }

  func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    print("hehe")
    location = locations.first?.coordinate
  }
}

In the view. I have some custom button that when clicked, will call

locationManager.requestLocation()

but it gives this error message

Updates[14291:194102] *** Assertion failure in -[CLLocationManager requestLocation], CLLocationManager.m:1339
 *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Delegate must respond to locationManager:didFailWithError:'
*** First throw call stack:
(
	0  CoreFoundation           0x0000000111b3a8cb __exceptionPreprocess + 242
	1  libobjc.A.dylib           0x000000010d4efba3 objc_exception_throw + 48
	2  Foundation             0x0000000116b3837c _userInfoForFileAndLine + 0
	3  CoreLocation            0x000000010c1baf65 CLClientStopVehicleHeadingUpdates + 52782

	7  SwiftUI               0x0000000113c6f5dc __swift_memcpy160_4 + 113486
	8  SwiftUI               0x0000000113c6fe8d __swift_memcpy160_4 + 115711
	9  SwiftUI               0x0000000113c6fdff __swift_memcpy160_4 + 115569
	10 SwiftUI               0x00000001140a4270 objectdestroy.142Tm + 41347
	11 SwiftUI               0x00000001140a4284 objectdestroy.142Tm + 41367
	12 SwiftUI               0x00000001140a4270 objectdestroy.142Tm + 41347
	13 SwiftUI               0x0000000113dba491 block_destroy_helper + 36990
	14 SwiftUI               0x0000000113db9df2 block_destroy_helper + 35295
	15 SwiftUI               0x0000000113f4b7a5 _callVisitToolbarContentType2 + 4011
	16 SwiftUI               0x00000001146fa7c8 _callVisitStyleContextType2 + 11258
	17 SwiftUI               0x00000001146f8e5e _callVisitStyleContextType2 + 4752
	18 SwiftUI               0x00000001146f8f42 _callVisitStyleContextType2 + 4980
	19 SwiftUI               0x00000001146f8720 _callVisitStyleContextType2 + 2898
	20 UIKitCore              0x0000000120b384b9 -[UIGestureRecognizer _componentsEnded:withEvent:] + 153
	21 UIKitCore              0x00000001211d7ebd -[UITouchesEvent _sendEventToGestureRecognizer:] + 662
	22 UIKitCore              0x0000000120b286f7 -[UIGestureEnvironment _updateForEvent:window:] + 469
	23 UIKitCore              0x000000012117aedb -[UIWindow sendEvent:] + 5282
	24 UIKitCore              0x000000012114e7f2 -[UIApplication sendEvent:] + 898
	25 UIKitCore              0x00000001211f5e61 __dispatchPreprocessedEventFromEventQueue + 9381
	26 UIKitCore              0x00000001211f8569 __processEventQueue + 8334
	27 UIKitCore              0x00000001211ee8a1 __eventFetcherSourceCallback + 272
	28 CoreFoundation           0x0000000111a9a035 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
	29 CoreFoundation           0x0000000111a99f74 __CFRunLoopDoSource0 + 157
	30 CoreFoundation           0x0000000111a997d1 __CFRunLoopDoSources0 + 308
	31 CoreFoundation           0x0000000111a93e73 __CFRunLoopRun + 927
	32 CoreFoundation           0x0000000111a936f7 CFRunLoopRunSpecific + 560
	33 GraphicsServices          0x000000011985b28a GSEventRunModal + 139
	34 UIKitCore              0x000000012112d62b -[UIApplication _run] + 994
	35 UIKitCore              0x0000000121132547 UIApplicationMain + 123
	36 SwiftUI               0x00000001146b2cfb __swift_memcpy93_8 + 10826
	37 SwiftUI               0x00000001146b2ba8 __swift_memcpy93_8 + 10487
	38 SwiftUI               0x0000000113d68b7d __swift_memcpy195_8 + 12271
	41 dyld                0x000000010be3d2bf start_sim + 10
	42 ???                 0x0000000203a3d310 0x0 + 8651002640
)
libc++abi: terminating with uncaught exception of type NSException
Message from debugger: Terminated due to signal 6
  1. I really have no clue what is going on. It does not enter some breakpoint, it just crashes. Anything I am missing?

  2. Also, i could not find anything in the LocationManager regarding a callback when the location is retrieved? or it is a manual workaround that has to be done for this case?

  3. the issue may be because i need to include the function

func locationManager( _ manager: CLLocationManager, didFailWithError error: Error )

but adds another question. Why do i get Error Domain=kCLErrorDomain Code=1 "(null)"

Answered by ForumsContributor in

added note is i already tried erasing content or simulator, the permission category in the info plist are there (its a swiftui project)

sadly no location swrvices in simulators even until ios16 so i couldnt even try to manually change it.

As the error message says:

Delegate must respond to locationManager:didFailWithError:

the issue may be because i need to include the function func locationManager( _ manager: CLLocationManager, didFailWithError error: Error )

Yes!

Accepted Answer

Weird that the solution was to onclude 2 permission. it never worked when i first added it.

later on i tried to do it again and all of a sudden it worked.

LocationManager Error when requesting location
 
 
Q