我試圖製作一個顯示信標鄰近度的應用程序。我做了一個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