【发布时间】:2015-10-18 21:55:47
【问题描述】:
我的代码包含在下面。这是一个旨在返回当前位置坐标的类。我得到的唯一输出是“before after before after”——requestAlwaysAuthorization 周围的打印行。然后应用程序崩溃。请求对话框有时会短暂显示,有时会显示几秒钟。在少数情况下,我什至可以按“确定”。应用程序总是崩溃。我在 xCode 7.0 和现在的 xCode 7.0.1、iOS 9.0 中都遇到了这个问题。我搜索了 StackOverflow 的高低,关于这个主题的大多数问题都是针对 xCode 和 iOS 的早期版本的,并且发布的解决方案对我的情况都没有帮助。因此,这个问题。我还发现了基本上可以完成我正在做的事情的 YouTube 教程,但没有乐趣。我的 plist 中也有 NSLocationAlwaysUsageDescription ,并且我有 Privacy - Location Usage Description 和 NSLocationWhenInUseUsageDescription 。我还尝试通过 Product\Scheme 菜单从 xCode 发送位置,并且我还尝试使用模拟器的 Debug\Location。我已经为每个尝试了几个不同的位置选项。模拟器的地图应用程序似乎总是可以工作。 Apple 的 LocateMe(用 Objective C 编写)也可以。 Swift 2(我的代码如下)失败。
import CoreLocation
class TheCurrentLocation: NSObject, CLLocationManagerDelegate {
var locationManager: CLLocationManager!
var latitude: Double = 0
var longitude: Double = 0
var locationStatus: NSString = "Not Started"
func initialize() {
self.locationManager = CLLocationManager()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
print("before")
self.locationManager.requestAlwaysAuthorization()
// self.locationManager.requestWhenInUseAuthorization()
print("after")
} // END: initialize()
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
self.locationManager.stopUpdatingLocation()
print("ERRORS: " + error.localizedDescription )
} // END: locationManager delegate didFailWithError
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.last
let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
self.locationManager.stopUpdatingLocation()
print ("ta da")
self.latitude = center.latitude
self.longitude = center.longitude
} // END: locationManager delegate didUpdateLocations
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
var isAllowed = false
switch status {
case CLAuthorizationStatus.Restricted:
locationStatus = "Restricted Access to Location"
case CLAuthorizationStatus.Denied:
locationStatus = "User Denied Access to Location"
case CLAuthorizationStatus.NotDetermined:
locationStatus = "Location Status Not Determined"
default:
locationStatus = "Allowed Access to Location"
isAllowed = true
} // END switch status
if (isAllowed == true) {
NSLog(String(locationStatus))
self.locationManager.startUpdatingLocation()
} else {
NSLog(String(locationStatus))
}
} // END: locationManager delegate didChangeAuthorizationStatus
} // END: theCurrentLocation
【问题讨论】:
-
用有关崩溃的详细信息更新您的问题。它在哪里崩溃,完整和准确的错误消息是什么?
-
没有错误信息。我希望我能告诉你更多。除了已经描述的打印行之外,没有其他输出。
-
@RobWelan 在 Xcode 中,当您的应用程序崩溃时,您可以通过在调试器中的
(lldb)提示符处键入bt来获取报告崩溃错误的回溯。 -
在“0x22888 : movl $0x0, (%esp)”行失败。此行会弹出“始终允许位置”对话框。然后 BOOM,崩溃。我尝试添加更多日志信息,但遇到了字符限制。 StackOverflow 中有没有办法添加更多内容(以防有人要求)?
-
@DanielZhang bt 似乎不起作用。一旦发生崩溃,就不再有调试提示。 (我有点菜鸟——我可能会遗漏一些明显的东西)。
标签: ios swift cllocationmanager