Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

requestLocation() always fails in the simulator with Swift 2

Swift 2 and watchOS2 has completely changed how Apple Watch extensions work and I have to now recreate them. Originally, I had requested the location on the watch in the normal fashion:

    self.locationManager.delegate = self
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationManager.requestAlwaysAuthorization()
    self.locationManager.beginUpdatingLocation()

Now in watchOS2, you may only request the locations one at a time, with "locationManager.requestLocation()". It is supposed to return a single location. Here is how I am using it:

override func awakeWithContext(context: AnyObject?) {
    super.awakeWithContext(context)

    self.locationManager.delegate = self
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationManager.requestAlwaysAuthorization()
    self.locationManager.requestLocation() //crashes right here

}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

}

func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
    print(error)
}

It crashes every time and I can't figure out why. I have cleaned the build, ensured that it is simulating a location (startUpdatingLocation() works on the parent app just fine). What do I do?

EDIT: Here is the stack trace:

2015-10-06 12:39:39.196 MyApp WatchKit    Extension[11486:2233547] *** Assertion failure in -[CLLocationManager requestLocation], /BuildRoot/Library/Caches/com.apple.xbs/Sources/CoreLocationFramework_Sim/CoreLocation-1861.1.12/Framework/CoreLocation/CLLocationManager.m:818
2015-10-06 12:39:39.198 MyApp WatchKit Extension[11486:2233547] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Delegate must respond to locationManager:didFailWithError:'
*** First throw call stack:
(
    0   CoreFoundation                      0x002eaaf4 __exceptionPreprocess + 180
1   libobjc.A.dylib                     0x021e0df4 objc_exception_throw + 50
2   CoreFoundation                      0x002ea98a +[NSException raise:format:arguments:] + 138
3   Foundation                          0x007e3ad0 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 118
4   CoreLocation                        0x006864ec CLClientGetCapabilities + 12846
5   MyApp WatchKit Extension    0x00067a45 _TFC32MyApp_WatchKit_Extension27distanceInterfaceController16awakeWithContextfS0_FGSqPSs9AnyObject__T_ + 341
6   MyApp WatchKit Extension    0x00067afa _TToFC32MyApp_WatchKit_Extension27distanceInterfaceController16awakeWithContextfS0_FGSqPSs9AnyObject__T_ + 58
7   WatchKit                            0x0010f396 _WKInterfaceControllerCreateClass + 482
8   WatchKit                            0x000f7dc8 __48-[SPRemoteInterface handlePlist:fromIdentifier:]_block_invoke_3 + 366
9   libdispatch.dylib                   0x0438e7b7 _dispatch_call_block_and_release + 15
10  libdispatch.dylib                   0x043ac40d _dispatch_client_callout + 14
11  libdispatch.dylib                   0x0439505a _dispatch_main_queue_callback_4CF + 689
12  CoreFoundation                      0x0023cbee __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 14
13  CoreFoundation                      0x001fa964 __CFRunLoopRun + 2356
14  CoreFoundation                      0x001f9d76 CFRunLoopRunSpecific + 470
15  CoreFoundation                      0x001f9b8b CFRunLoopRunInMode + 123
16  Foundation                          0x00777601 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 308
17  Foundation                          0x0081d9cd -[NSRunLoop(NSRunLoop) run] + 82
18  libxpc.dylib                        0x046910b7 _xpc_objc_main + 486
19  libxpc.dylib                        0x04693e16 xpc_main + 215
20  Foundation                          0x00946c45 service_connection_handler + 0
21  PlugInKit                           0x038d81f4 -[PKService run] + 582
22  WatchKit                            0x0011df71 main + 146
23  libdyld.dylib                       0x043d0ae1 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 
like image 224
MantisShrimp Avatar asked Oct 06 '15 16:10

MantisShrimp


1 Answers

The error message tells you that you have not implemented a delegate method:

Delegate must respond to locationManager:didFailWithError:
like image 135
Droppy Avatar answered Oct 14 '22 17:10

Droppy