ibeacon – 用于Beacon检测的Swift2.3代码

前端之家收集整理的这篇文章主要介绍了ibeacon – 用于Beacon检测的Swift2.3代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我们正处于开发Swift2.2应用程序的高级阶段,因此决定在此期间迁移到2.3并稍后进行完整的Swift 3迁移.但是我们无法在转换到Swift 2.3后获得信标检测工作.方法“didRangeBeacons”不断返回一个空数组.相同的代码在Swift 2.2中工作,因此我们知道我们拥有所有权限等.

此外,如果我们在同一个ipad上打开“定位”应用程序,那么我们的应用程序也会开始在“didRangeBeacons”中返回数据.尝试了各种版本的应用程序,所有Swift2.3应用程序的行为方式都相同.无法弄清楚Locate应用程序正在做什么…在同一条船上的任何人?

这是我们正在使用的代码.我不确定这应该写在这里或评论中,但不能以某种方式将代码放在评论中……

  1. import UIKit
  2. import CoreLocation
  3.  
  4. class ViewController: UIViewController,CLLocationManagerDelegate {
  5.  
  6. let locationManager = CLLocationManager()
  7. let region = CLBeaconRegion(proximityUUID: NSUUID(UUIDString: "9735BF2A-0BD1-4877-9A4E-103127349E1D")!,identifier: "testing")
  8. // Note: make sure you replace the keys here with your own beacons' Minor Values
  9.  
  10.  
  11. override func viewDidLoad() {
  12. super.viewDidLoad()
  13. // Do any additional setup after loading the view,typically from a nib.
  14. self.locationManager.delegate = self
  15. self.locationManager.requestAlwaysAuthorization()
  16. self.locationManager.startMonitoringForRegion(self.region)
  17. }
  18.  
  19. override func didReceiveMemoryWarning() {
  20. super.didReceiveMemoryWarning()
  21. // Dispose of any resources that can be recreated.
  22. }
  23.  
  24. func locationManager(manager: CLLocationManager,didStartMonitoringForRegion region: CLRegion) {
  25. print("didStartMonitoringForRegion")
  26. self.locationManager.requestStateForRegion(region)
  27. }
  28.  
  29. func locationManager(manager: CLLocationManager,monitoringDidFailForRegion region: CLRegion?,withError error: NSError) {
  30. print("monitoringDidFailForRegion")
  31. }
  32.  
  33. func locationManager(manager: CLLocationManager,didDetermineState state: CLRegionState,forRegion region: CLRegion) {
  34. print("didDetermineState")
  35. if state == .Inside {
  36. //Start Ranging
  37. self.locationManager.startRangingBeaconsInRegion(self.region)
  38. self.locationManager.startUpdatingLocation()
  39. }
  40. else {
  41. //Stop Ranging here
  42. self.locationManager.stopUpdatingLocation()
  43. self.locationManager.stopRangingBeaconsInRegion(self.region)
  44. }
  45. }
  46.  
  47. func locationManager(manager: CLLocationManager,didRangeBeacons beacons: [CLBeacon],inRegion region: CLBeaconRegion) {
  48. print(beacons.count)
  49. }

}

[更新发布更多尝试以使其工作]
如果我们删除self.locationManager.startMonitoringForRegion(self.region),应用程序在前台模式下工作
调用self.locationManager.startRangingBeaconsInRegion(self.region)
self.locationManager.requestAlwaysAuthorization()之后直接

这是次优的,因为我们没有进入和退出事件或状态,但至少我们得到了信标计数.

@H_301_16@
@H_301_16@
在iOS 10上有许多关于信标检测问题的轶事报告.症状包括

>不正确的区域退出事件,尤其是当应用程序在后台时,如果按下肩部按钮,则会显示输入事件.
>远程信标检测中的周期性丢失,当信标在附近时,回调提供空信标列表.
>当针对iOS 9.x运行不同的信标测距或检测应用程序时,远程信标回调会返回正确的结果.

这可能是一个将在iOS更新中修复的错误.在此之前,一些用户已经报告将XCode中的应用程序部署目标设置为9.x将解决该问题.

@H_301_16@

猜你在找的Swift相关文章