Onichan
Onichan

Reputation: 4526

Location Manager Crashing when No Internet

The program runs fine when the user is connected to the internet. However, when the user is not connected, the app crashes with:

Error:The operation couldn’t be completed. (kCLErrorDomain error 8.)
fatal error: unexpectedly found nil while unwrapping an Optional value

I'd like the catch the error before it crashes and display an alertcontroller

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: { (placemarks, error) -> Void in
        if (error != nil) {
            println("Error:" + error.localizedDescription)
        }
        if placemarks.count > 0 {
            let pm = placemarks[0] as CLPlacemark
            self.displayLocationInfo(pm)
            currentLoc = manager.location
            currentLocGeoPoint = PFGeoPoint(location:currentLoc)
            var query = PFQuery(className:"Restaurant") //default: Restaurant
            query.whereKey("RestaurantLoc", nearGeoPoint:currentLocGeoPoint, withinMiles:setDistanceMiles) //filter by miles
            query.limit = 1000 //limit number of results
            query.findObjectsInBackgroundWithBlock {
                (objects: [AnyObject]!, error: NSError!) -> Void in
                if objects.isEmpty {
                    //NO RESULTS
                    let alertController = UIAlertController(title: "Unavailable Area", message:
                        "This app is not available in your area yet. We'll try our best to have it up and running as soon as possible.", preferredStyle: UIAlertControllerStyle.Alert)
                    alertController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default,handler: nil))
                    self.presentViewController(alertController, animated: true, completion: nil)
                    self.navigationController!.popViewControllerAnimated(true)
                } else {
                    newArray = objects
                }
                self.hideActivityIndicator(self.view)
            }
        } else {
            println("error: \(error)")
        }
    })
}

enter image description here

Upvotes: 0

Views: 90

Answers (2)

matt
matt

Reputation: 535556

The problem is your if statement. You are saying this:

if (error != nil) {
    println("Error:" + error.localizedDescription)
}
if placemarks.count > 0 { // ...

Well, there was an error. And you were told that there was an error. But you didn't listen! You didn't stop! You just go right on as if nothing had happened, and try to access the placemarks variable - which wasn't returned. You need to use logic here so that you exit if there's an error:

if (error != nil) {
    println("Error:" + error.localizedDescription)
    return // go no further!
}
if placemarks.count > 0 { // ...

Note, however, that it would be better to check placemarks for nil first:

if placemarks == nil {
    // check error
    // return
}
if placemarks.count > 0 { // ...

Upvotes: 2

Faiz Mokhtar
Faiz Mokhtar

Reputation: 948

I believe you need to implement locationManager:didFailWithError which tells the delegate when it is unable to retrieve the location. Then, you just need to handle the error with appropriate response.

For references: CLLocationManagerDelegate apple docs

Upvotes: 1

Related Questions