我试图制作一个显示信标邻近度的应用程序。我做了一个label
来接收这些数据,但是我不能,而且我想只显示“接近”而不是显示在控制台中的所有这些数据。我尝试使用beacons[3]
,但该程序给我一个错误。控制台打印用Swift 2标记
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet var metrosBeacon: UILabel!
let locationManager = CLLocationManager()
let region = CLBeaconRegion(proximityUUID: NSUUID(UUIDString: "FDA50693-A4E2-4FB1-AFCF-C6EB07647828")!, identifier: "MKT BEACONS")
// Note: make sure you replace the keys here with your own beacons' Minor Values
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager.delegate = self
if (CLLocationManager.authorizationStatus() != CLAuthorizationStatus.AuthorizedWhenInUse) {
locationManager.requestWhenInUseAuthorization()
}
locationManager.startRangingBeaconsInRegion(region)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], inRegion region: CLBeaconRegion){
print (beacons)
metrosBeacon.text = "/(beacons)"
}}
控制台数据:
[CLBeacon(UUID:< __NSConcreteUUID 0x12ee586c0> FDA50693-A4E2-4FB1-AFCF-C6EB07647828,主要:10004,次要:54480,接近:1 +/- 0.05m,rssi:-32)]
谢谢大家!
你打电话得到的错误标[3]可能会发生,因为比少四个信标是可见的。我会在他的答案中使用@ eric-d显示的解决方案,您可以访问beacons.first来获取第一个,然后访问其邻近区域。 – davidgyoung