【发布时间】:2020-12-26 20:31:54
【问题描述】:
我遵循了一个在线教程,并且在我的项目中运行了以下代码,它可以检测具有特定 uuid/major/minor 的 iBeacon 并对其进行一些逻辑处理。我想知道是否有一种方法可以接受多个 uuid 或多个主要/次要并将它们传递给其他功能?这是我到目前为止的代码:
class BeaconDetector: NSObject, ObservableObject, CLLocationManagerDelegate {
var locationManager: CLLocationManager?
@Published var lastDistance = CLProximity.unknown
override init(){
super.init()
locationManager = CLLocationManager()
locationManager?.delegate = self
locationManager?.requestAlwaysAuthorization()
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus){
if status == .authorizedAlways{
if CLLocationManager.isMonitoringAvailable(for: CLBeaconRegion.self) {
if CLLocationManager.isRangingAvailable(){
startScanning()
}
}
}
}
func startScanning(){
let uuid = UUID(uuidString: "2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6")!
let constraint = CLBeaconIdentityConstraint(uuid: uuid, major: 0, minor: 0)
let beaconRegion = CLBeaconRegion(beaconIdentityConstraint: constraint, identifier: "MyBeacon")
locationManager?.startMonitoring(for: beaconRegion)
locationManager?.startRangingBeacons(satisfying: constraint)
}
func locationManager(_ manager: CLLocationManager, didRange beacons: [CLBeacon], satisfying beaconConstraint: CLBeaconIdentityConstraint) {
if let beacon = beacons.first{
update(distance: beacon.proximity)
} else{
update(distance: .unknown)
}
}
func update(distance: CLProximity) {
lastDistance = distance
}
}
我想我知道如何通过将值添加到更新函数来传递值,但是我如何能够检测和接受多个信标将是我现在最大的问题,谢谢!
【问题讨论】:
标签: ios swift core-location ibeacon