【发布时间】:2018-08-10 12:30:11
【问题描述】:
我正在尝试在后台将位置更新发送到服务器
应用有时在后台崩溃
这是我的locationmanager delegate
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let lat = manager.location?.coordinate.latitude,
let long = manager.location?.coordinate.longitude {
glat = String(lat)
glong = String(long)
if UIApplication.shared.applicationState == .background {
self.updatebackloc(lat, long: long)
}
if UIApplication.shared.applicationState == .active {
self.updateloc(lat, long: long)
locationManager.stopUpdatingLocation()
let status = CLLocationManager.authorizationStatus()
if (status == CLAuthorizationStatus.authorizedAlways) {
locationManager.startMonitoringSignificantLocationChanges()
}
}
}
}
这是updatebacklog函数
func updatebackloc(_ lat: CLLocationDegrees, long: CLLocationDegrees) {
let userID = TegKeychain.get("userID")!
let parameters: Parameters = ["userID": userID, "lat": lat, "long":long]
Alamofire.request("https://xxxxx.com/ios/updatebackloc.php", method: .post, parameters: parameters).validate().responseJSON { response in
switch response.result {
case .success:
if let json = response.result.value {
var success = 0
if let dictJSON = json as? [String: AnyObject] {
if let successInteger = dictJSON["success"] as? Int {
success = successInteger
if success == 1
{
}
}
}
}
case .failure(_):
return
}
}
}
didFinishLaunchingWithOptions 部分
if let _ = launchOptions?[UIApplicationLaunchOptionsKey.location] {
startSignificationLocation()
}
startSignificationLocation 函数在didFinishLaunchingWithOptions 上触发
func startSignificationLocation() {
let locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.allowsBackgroundLocationUpdates = true
locationManager.startMonitoringSignificantLocationChanges()
}
这是崩溃日志
Crashed: com.apple.main-thread
0 0x10285b798 specialized AppDelegate.updatebackloc(Double, long : Double) -> () + 4339644312
1 0x10285b8e4 specialized AppDelegate.locationManager(CLLocationManager, didUpdateLocations : [CLLocation]) -> () (AppDelegate.swift:396)
2 0x1028540c0 @objc AppDelegate.locationManager(CLLocationManager, didUpdateLocations : [CLLocation]) -> () (AppDelegate.swift)
3 CoreLocation 0x1874f97bc (null) + 77412
4 CoreLocation 0x1874f901c (null) + 75460
5 CoreLocation 0x1874e16b4 (null) + 1004
6 CoreFoundation 0x180ea3590 __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 20
7 CoreFoundation 0x180ea2e60 __CFRunLoopDoBlocks + 288
8 CoreFoundation 0x180ea10c8 __CFRunLoopRun + 2436
9 CoreFoundation 0x180dc0c58 CFRunLoopRunSpecific + 436
10 GraphicsServices 0x182c6cf84 GSEventRunModal + 100
11 UIKit 0x18a5195c4 UIApplicationMain + 236
12 0x1027fe524 main (InboxInterests.swift:30)
13 libdyld.dylib 0x1808e056c start + 4
这里是代码
编码为文本
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let lat = manager.location?.coordinate.latitude,
let long = manager.location?.coordinate.longitude {
glat = String(lat)
glong = String(long)
if UIApplication.shared.applicationState == .background {
self.updatebackloc(lat, long: long)
}
if UIApplication.shared.applicationState == .active {
self.updateloc(lat, long: long)
locationManager.stopUpdatingLocation()
let status = CLLocationManager.authorizationStatus()
if (status == CLAuthorizationStatus.authorizedAlways) {
locationManager.startMonitoringSignificantLocationChanges()
}
}
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error)
}
func updateloc(_ lat: CLLocationDegrees, long: CLLocationDegrees) {
let userID = TegKeychain.get("userID")!
let parameters: Parameters = ["userID": userID, "lat": lat, "long":long]
Alamofire.request("https://xxxxx.com/ios/updateloc.php", method: .post, parameters: parameters).validate().responseJSON { response in
switch response.result {
case .success:
if let json = response.result.value {
var success = 0
if let dictJSON = json as? [String: AnyObject] {
if let successInteger = dictJSON["success"] as? Int {
success = successInteger
if success == 1
{
}
}
}
}
case .failure(_):
return
}
}
}
func updatebackloc(_ lat: CLLocationDegrees, long: CLLocationDegrees) {
guard let userID = TegKeychain.get("userID") else {
return
}
let parameters: Parameters = ["userID": userID, "lat": lat, "long":long]
Alamofire.request("https://xxxxx.com/ios/updatebackloc.php", method: .post, parameters: parameters).validate().responseJSON { response in
switch response.result {
case .success:
if let json = response.result.value {
var success = 0
if let dictJSON = json as? [String: AnyObject] {
if let successInteger = dictJSON["success"] as? Int {
success = successInteger
if success == 1
{
}
}
}
}
case .failure(_):
return
}
}
}
【问题讨论】:
-
你能定位崩溃发生的位置吗?哪个语句抛出异常?
-
抛出异常的代码行会有帮助。
-
只有锁定设备才会崩溃?
-
@shallowThought 它在后台崩溃
-
"AppDelegate.swift:436" 您发布的代码中的第 436 行是哪一行?
标签: ios swift crash cllocationmanager