Reputation: 117
It's weird. There are some devices that crash and some other devices that not. The thing is when having location not activated the app never dies but when I allow my app access to the location in some devices crash and in other devices not.
This is the code:
override func viewDidAppear(animated: Bool) {
if CLLocationManager.locationServicesEnabled(){
switch CLLocationManager.authorizationStatus() {
case .NotDetermined, .Restricted, .Denied:
print("No access")
case .AuthorizedAlways, .AuthorizedWhenInUse:
let geocoder = CLGeocoder()
longitude = self.locationManager.location!.coordinate.longitude
latitude = self.locationManager.location!.coordinate.latitude
geocoder.reverseGeocodeLocation(CLLocation(latitude: (latitude), longitude: (longitude)), completionHandler: {placemarks, error in
if error == nil && placemarks!.count > 0 {
self.thoroughfare = (placemarks!.last?.thoroughfare)!
self.city = (placemarks!.last?.locality)!
print(self.thoroughfare)
print(self.city)
print(self.longitude)
print(self.latitude)
}
})
}
} else {
print("Location services are not enabled")
}
}
When app crashes the error points to this line:
longitude = self.locationManager.location!.coordinate.longitude
latitude = self.locationManager.location!.coordinate.latitude
I've tested the app in 10 devices, having 1-2 of them that crashes at this point.
What's happening? I think I'm managing rightly what to do and what no to do when location is or not is allowed.
Upvotes: 0
Views: 4029
Reputation: 475
Please try this whole code to get the location and its details.Its tried and working solution in Swift 3.0
import CoreLocation
import Foundation
class ViewController: UIViewController,CLLocationManagerDelegate {
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
findMyLocation()
}
func findMyLocation(){
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: {(placemarks, error)->Void in
if (error != nil) {
print("Reverse geocoder failed with error" + error!.localizedDescription)
return
}
if placemarks!.count > 0 {
let pm = placemarks![0]
self.displayLocationInfo(pm)
} else {
print("Problem with the data received from geocoder")
}
})
}
func displayLocationInfo(_ placemark: CLPlacemark?) {
if let containsPlacemark = placemark {
//stop updating location to save battery life
locationManager.stopUpdatingLocation()
let locality = (containsPlacemark.locality != nil) ? containsPlacemark.locality : ""
let postalCode = (containsPlacemark.postalCode != nil) ? containsPlacemark.postalCode : ""
let administrativeArea = (containsPlacemark.administrativeArea != nil) ? containsPlacemark.administrativeArea : ""
let country = (containsPlacemark.country != nil) ? containsPlacemark.country : ""
print(" Postal Code \(postalCode)")
print(" administrativeArea \(administrativeArea)")
print(" country \(country)")
print(" locality \(locality)")
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Error while updating location " + error.localizedDescription)
}
Thank you
Upvotes: 1
Reputation: 853
Please Check your didUpdateLocations method, you just need to check whether location is getting correctly or getting nil.
if ((self.locationManager.location) != nil){
//Get Location access here.
}
Upvotes: 0
Reputation: 5658
Don't declare optional values to variable. always handle errors Don't unwrap the location
self.locationManager.location // Your error
if var longitude = self.locationManager.location.coordinate.longitude {
// do your thing
}else {
// handle the error by declaring default value
}
second thing you also might receive null values even if user lost internet while getting the location or you forget the simulate the location while testing in simulator so always handle the error
Upvotes: 0
Reputation: 1589
You should chek if
self.locationManager.location
Is null before using it
Upvotes: 4