首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于信标检测的Swift2.3代码

用于信标检测的Swift2.3代码
EN

Stack Overflow用户
提问于 2016-09-22 11:43:04
回答 3查看 2.7K关注 0票数 1

我们正处于开发Swift2.2应用程序的高级阶段,因此决定在此期间迁移到2.3,并在以后进行完整的Swift 3迁移。然而,我们无法获得信标检测工作后转换为Swift 2.3。方法"didRangeBeacons“一直返回一个空数组。同样的代码在SWIFT2.2中运行,所以我们知道我们有所有的权限等等。

另外,如果我们在同一台ipad上打开"Locate“应用程序,那么我们的应用程序也会开始返回"didRangeBeacons”中的数据。已经尝试过不同版本的应用程序,而且所有的Swift2.3应用程序的行为都是一样的。不知道定位程序在做什么.有谁在同一条船上吗?

这是我们正在使用的代码。我不确定这是否应该写在这里或注释中,但无法将代码放入注释中.

代码语言:javascript
复制
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.requestAlwaysAuthorization()之后直接调用self.locationManager.startRangingBeaconsInRegion(self.region),那么更新发布了更多的尝试,以使这个工作的应用程序在前台模式下工作

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

EN

回答 3

Stack Overflow用户

发布于 2016-09-22 13:57:46

有许多关于iOS 10信标检测问题的传闻报道。症状包括:

  • 不正确的区域退出事件,特别是当应用程序在后台时,如果按下肩键,则后面是入口事件。
  • 在远程信标探测中的周期性退出,当信标在附近时,回调提供一个空信标列表。
  • 远程信标回调在运行针对iOS 9.x的不同信标测距或检测应用程序时返回适当的结果。

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

票数 2
EN

Stack Overflow用户

发布于 2016-09-23 12:59:59

尝试在加载视图后构造您的位置管理器。换言之,改变:

代码语言:javascript
复制
let locationManager = CLLocationManager()

代码语言:javascript
复制
let locationManager : CLLocationManager!

然后将其添加到viewDidLoad中。

代码语言:javascript
复制
locationManager = CLLocationManager()

我已经看到了在初始化LocationManager的基础上构造UIViewController的奇怪行为。

票数 0
EN

Stack Overflow用户

发布于 2017-02-11 08:45:52

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39638067

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档