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

时间:2015-06-12 20:56:26

标签: ios swift xcode6 bluetooth-lowenergy core-location

我一直在使用一些教程,并且每个教程都持续存在完全相同的问题。当我在其他教程中运行以下代码或其他版本时,我会继续:

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

我将(区域)切换到(区域!)并获得一个输出,说明在解开意外的零时发生错误。

我已经激怒了,并尝试了很多可能性。有谁知道问题是什么?它会在屏幕加载时崩溃。

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 个答案:

答案 0 :(得分:0)

这是我的代码版本,带有正确的签名。请注意,NSUUID:UUIDString是一个可以返回nil的可选初始化程序。

导入UIKit 导入CoreLocation

类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将返回nil。