使用 Swift 在 CoreLocation 中搜索区域的正确方法是什么?

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

我一直在研究许多教程,并且每个教程都不断遇到完全相同的问题。当我 运行 在其他教程中使用以下代码或它的其他形式时,我不断得到:

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

我将 (region) 切换到 (region!) 并得到一个输出,指出在解包一个意外的 nil 时发生错误。

我非常恼火,尝试了多种可能性。有谁知道问题出在哪里?它在屏幕加载时崩溃。

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)
    }

}

这是我的带有正确签名的代码版本。请注意 NSUUID:UUIDString 是一个可选的初始化器,它可以 return nil.

导入 UIKit 导入 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)
}

}

运行 看看你是哪里破的?你所在的地区是零吗?因为如果您的 NSUUID 格式不正确,CLBeaconRegion 将 return nil。