I am trying to send the location update to the server in the background
App sometimes crashes in the background
Here is my 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()
}
}
}
}
This is the updatebacklog function
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
part
if let _ = launchOptions?[UIApplicationLaunchOptionsKey.location] {
startSignificationLocation()
}
startSignificationLocation
function triggered on didFinishLaunchingWithOptions
func startSignificationLocation() {
let locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.allowsBackgroundLocationUpdates = true
locationManager.startMonitoringSignificantLocationChanges()
}
Here is the crash log
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
Here is the code
code as text
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
}
}
}