问题描述
我是快速编程的新手,我正在尝试开发应用程序检测信标但仍然无法检测到任何信标。
I am new to swift programing , I am trying to develop app detecting beacons but still I can not detect any beacon.
let region = CLBeaconRegion(proximityUUID: NSUUID(UUIDString: "11111111-1111-1111-1111-111111111111")!,major: 1, minor: 1, identifier: "MyBeacon")
locationManager.startMonitoringForRegion(region)
locationManager.startRangingBeaconsInRegion(region)
推荐答案
首先,您应该添加 CoreLocation.Framework
-
在.Plist文件中添加密钥/字符串
NSLocationAlwaysUsageDescription
string
在你的对象中添加 CLLocationManagerDelegate
在此示例中添加 CLLocationManagerDelegate
委托方法我将添加didRangeBeacons方法
Add the CLLocationManagerDelegate
delegate methods in this example i will just add the didRangeBeacons method
func locationManager(_ manager: CLLocationManager, didRangeBeacons
beacons: [CLBeacon], in region: CLBeaconRegion) {
print(beacons) }
-
创建并初始化locationManager
Create and initialise the locationManager
var locationManager:CLLocationManager = CLLocationManager()
创建
CLBeaconRegion
let beaconRegion : CLBeaconRegion = CLBeaconRegion( proximityUUID: NSUUID.init(uuidString:"****-****-****-****-******") as! UUID, identifier: "my beacon")
-
将代表添加到您的对象
locationManager.delegate = self
现在让我们开始范围
locationManager.startRangingBeacons(in: beaconRegion)
这将自动调用委托方法
func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) { print(beacons) }
注意:如果你想监控beacon的进入/退出状态,你需要添加
Note : if you want to monitor beacon's entry/ exit state you need to add
locationManager.startMonitoring(for: beaconRegion)
最后:确保你的信标打开,你正在测试iBeacon帧享受:D
Finally : Make sure that your beacon is turned ON and you are testing the iBeacon Frame Enjoy :D
这篇关于一个简单的代码,用于检测swift中的任何信标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!