requestLocation() 在模拟器中总是失败 Swift 2
requestLocation() always fails in the simulator with Swift 2
Swift 2 和 watchOS2 已经完全改变了 Apple Watch 扩展的工作方式,我现在必须重新创建它们。本来,我是用正常的方式在手表上请求位置的:
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestAlwaysAuthorization()
self.locationManager.beginUpdatingLocation()
现在在 watchOS2 中,您一次只能请求一个位置,"locationManager.requestLocation()"。它应该 return 一个位置。以下是我的使用方法:
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)
}
每次都崩溃,我不知道为什么。我已经清理了构建,确保它正在模拟一个位置(startUpdatingLocation() 在父应用程序上工作得很好)。我该怎么办?
编辑:这是堆栈跟踪:
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)
错误信息告诉你你还没有实现委托方法:
Delegate must respond to locationManager:didFailWithError:
Swift 2 和 watchOS2 已经完全改变了 Apple Watch 扩展的工作方式,我现在必须重新创建它们。本来,我是用正常的方式在手表上请求位置的:
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestAlwaysAuthorization()
self.locationManager.beginUpdatingLocation()
现在在 watchOS2 中,您一次只能请求一个位置,"locationManager.requestLocation()"。它应该 return 一个位置。以下是我的使用方法:
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)
}
每次都崩溃,我不知道为什么。我已经清理了构建,确保它正在模拟一个位置(startUpdatingLocation() 在父应用程序上工作得很好)。我该怎么办?
编辑:这是堆栈跟踪:
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)
错误信息告诉你你还没有实现委托方法:
Delegate must respond to locationManager:didFailWithError: