【问题标题】:Swift2.3 code for Beacon detectionBeacon检测的Swift2.3代码
【发布时间】:2023-03-19 05:30:01
【问题描述】:

我们正处于开发 Swift2.2 应用程序的后期阶段,因此决定暂时迁移到 2.3,稍后再进行完整的 Swift 3 迁移。但是,我们无法在转换为 Swift 2.3 后使信标检测正常工作。 “didRangeBeacons”方法不断返回一个空数组。相同的代码在 Swift 2.2 中运行,所以我们知道我们拥有所有权限等。

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


这是我们正在使用的代码。我不确定这应该写在这里还是在 cmets 中,但无法以某种方式将代码放入 cmets...

import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

let locationManager = CLLocationManager()
let region = CLBeaconRegion(proximityUUID: NSUUID(UUIDString: "9735BF2A-0BD1-4877-9A4E-103127349E1D")!, identifier: "testing")
// 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.
    self.locationManager.delegate = self
    self.locationManager.requestAlwaysAuthorization()
    self.locationManager.startMonitoringForRegion(self.region)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func locationManager(manager: CLLocationManager, didStartMonitoringForRegion region: CLRegion) {
    print("didStartMonitoringForRegion")
    self.locationManager.requestStateForRegion(region)
}

func locationManager(manager: CLLocationManager, monitoringDidFailForRegion region: CLRegion?, withError error: NSError) {
    print("monitoringDidFailForRegion")
}

func locationManager(manager: CLLocationManager, didDetermineState state: CLRegionState, forRegion region: CLRegion) {
    print("didDetermineState")
    if state == .Inside {
        //Start Ranging
        self.locationManager.startRangingBeaconsInRegion(self.region)
        self.locationManager.startUpdatingLocation()
    }
    else {
        //Stop Ranging here
        self.locationManager.stopUpdatingLocation()
        self.locationManager.stopRangingBeaconsInRegion(self.region)
    }
}

func locationManager(manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], inRegion region: CLBeaconRegion) {
    print(beacons.count)
}

}

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

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

【问题讨论】:

  • 你能展示你设置测距的代码吗?这些症状是在前台和后台都发生还是仅在后台发生?
  • 感谢您的回复!我们在后台看到更多问题。我们确实在 Info.pList 中设置了“隐私 - 位置始终使用说明”。还增加了后台功能。在后台时,信标计数始终通知为 0。
  • 代码如下
  • 我可以确认问题,但使用 iPhone 6S 和 iOS 10 完全没有问题

标签: ibeacon ios10 swift2.3


【解决方案1】:

有许多关于 iOS 10 上的信标检测问题的轶事报告。症状包括:

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

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

【讨论】:

  • 谢谢大卫。明天试试这个,然后回复你。
  • iOS10 肯定有问题。我们在 iOS 9 和 iOS10 设备上都运行了 Locate 应用程序,并且 iOS10 设备不断检测到 0 个信标。 iOS 9 的信号相当强。 1.我们删除了所有后台模式,仅使用了“使用时授权” 2.从 ios 10 设备中删除了所有其他信标检测软件 3.从设置->常规->重置中重置网络设置 经过一些修补后,我们有了该应用程序至少与 iOS 10 上的定位应用程序一样工作,但仅在前台模式下。以下帮助
  • 将部署目标设置为 9.x 未能解决问题。即使在前台,我们也可以使用我们需要的startMonitoringForRegion,因为我们想要跟踪退出和进入事件。从didDeterminState 中调用startrangingbeaconsinregion,其中state=Inside 会导致检测到信标= 0。如果我们像上面的代码一样独立调用startrangingbeaconsinregion,它会起作用..
  • 安装 10.0.3 (14A551) 后 iBeacon 问题较少
【解决方案2】:

尝试在视图加载后构建您的位置管理器。换句话说,改变:

let locationManager = CLLocationManager()

let locationManager : CLLocationManager!

然后将此添加到viewDidLoad:

locationManager = CLLocationManager()

我看到在 UIViewController 初始化时构造 LocationManager 的奇怪行为。

【讨论】:

    【解决方案3】:

    在测试信标测距时,我下载了多个忘记卸载的应用程序。在我卸载了所有与信标相关的应用程序并重新安装了所需的应用程序后,我就可以让测距信标工作了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多