我已经写了下面的代码
var locationManager: CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.pausesLocationUpdatesAutomatically = false
locationManager.allowsBackgroundLocationUpdates = true
locationManager.requestAlwaysAuthorization()
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse {
if CLLocationManager.isMonitoringAvailable(for: CLBeaconRegion.self) {
if CLLocationManager.isRangingAvailable() {
startScanning()
}
}
}
}
func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {
if beacons.count > 0 {
NSLog("Found beacon")
} else {
NSLog("Beacon not found")
}
}
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
let beaconRegion = region as! CLBeaconRegion
print("Did enter region: " + (beaconRegion.major?.stringValue)!)
}
func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
let beaconRegion = region as! CLBeaconRegion
print("Did exit region: " + (beaconRegion.major?.stringValue)!)
}
func startScanning() {
let uuid = UUID(uuidString: "B9407F30-F5F8-466E-AFF9-25556B57FE6D")!
let beaconRegion = CLBeaconRegion(proximityUUID: uuid, major: CLBeaconMajorValue(0), minor: CLBeaconMinorValue(1), identifier: "MyBeacon")
beaconRegion.notifyEntryStateOnDisplay = true
beaconRegion.notifyOnEntry = true
beaconRegion.notifyOnExit = true
locationManager.startMonitoring(for: beaconRegion)
locationManager.startRangingBeacons(in: beaconRegion)
}
}
我正在使用Xcode 11.2和Swive 5。我已经面对这个问题很多天了,我读了那么多关于苹果的评论和文件,但没有得到解决。如果有任何一个与iBeacons一起工作,那么请给出ANS,如果您有代码演示,也可以与我分享。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。