What is the proper way to search for regions in CoreLocation with Swift?

868 views Asked by At

I've been working with a number of tutorials and continuously have the exact same issue with each of them. When I run the following code or other incarnations of it in other tutorials, I keep getting:

2015-06-12 13:50:33.797 Scanner[4599:2593734] *** Assertion failure in -[CLLocationManager startRangingBeaconsInRegion:], /SourceCache/CoreLocationFramework/CoreLocation-1756.0.20/Framework/CoreLocation/CLLocationManager.m:1129
2015-06-12 13:50:33.798 Scanner[4599:2593734] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: region != nil'
*** First throw call stack:
(0x183efc2d8 0x1957200e4 0x183efc198 0x184db0ed4 0x18464f0d4 0x10008bebc 0x10008bf60 0x18893cc84 0x18893c994 0x1889431d0 0x188940880 0x1889b28ec 0x188bc6a94 0x188bc9208 0x188bc7778 0x18c7053c8 0x183eb427c 0x183eb3384 0x183eb19a8 0x183ddd2d4 0x1889a843c 0x1889a2fac 0x1000923b4 0x195d9ea08)
libc++abi.dylib: terminating with uncaught exception of type NSException

I switch (region) to (region!) and get an output saying that an error occurred while unwrapping an unexpected nil.

I'm beyond exasperated and have tried a number of possibilities. Does anyone know what the problem is? It crashes as the screen loads.

import UIKit
import CoreLocation


class ViewController: UIViewController, CLLocationManagerDelegate {
    let locationManager = CLLocationManager()
    let region = CLBeaconRegion(proximityUUID: NSUUID(UUIDString: "1800"), identifier: "BLE113")

    override func viewDidLoad(){
        super.viewDidLoad()

        locationManager.delegate = self;

        if (CLLocationManager.authorizationStatus() != CLAuthorizationStatus.AuthorizedWhenInUse) {
        locationManager.requestWhenInUseAuthorization()
        }

        locationManager.startRangingBeaconsInRegion(region)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func locationManager(manager: CLLocationManager!, didRangeBeacons beacons: [AnyObject]!, inRegion region: CLBeaconRegion!) {
        println(beacons)
    }

}
1

There are 1 answers

8
John Difool On BEST ANSWER

Here is my version of your code with the correct signature. Note that NSUUID:UUIDString is an optional initializer that can return nil.

import UIKit import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

let locationManager = CLLocationManager()
var region: CLBeaconRegion?
override func viewDidLoad(){
    if let proximityUUID = NSUUID(UUIDString: "40ACF125-D06F-492D-BD4F-0FC9D93F906C") { // generated using uuidgen tool

        self.region = CLBeaconRegion(proximityUUID: proximityUUID, identifier: "BLE113")
        if let region = self.region {
            locationManager.delegate = self

            if (CLLocationManager.authorizationStatus() != CLAuthorizationStatus.AuthorizedWhenInUse) {
                locationManager.requestWhenInUseAuthorization()
            }

            locationManager.startRangingBeaconsInRegion(region)
        }
    }
    super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}


func locationManager(manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], inRegion region: CLBeaconRegion) {
    print(beacons)
}

}

Run it and see where you are breaking? Is your region nil? Because CLBeaconRegion will return nil if your NSUUID is not correctly formed.