我们正处于开发 Swift2.2 应用程序的后期阶段,因此决定暂时迁移到 2.3,稍后再进行完整的 Swift 3 迁移。但是,我们无法在转换为 Swift 2.3 后使信标检测正常工作。“didRangeBeacons”方法不断返回一个空数组。相同的代码在 Swift 2.2 中运行,所以我们知道我们拥有所有权限等。
此外,如果我们在同一个 ipad 上打开“Locate”应用程序,那么我们的应用程序也会开始在“didRangeBeacons”中返回数据。已经尝试了各种版本的应用程序,所有 Swift2.3 应用程序的行为方式都相同。无法弄清楚 Locate 应用程序在做什么......同一条船上的任何人?
这是我们正在使用的代码。我不确定这应该写在这里或评论中,但无法以某种方式将代码放在评论中......
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),则应用程序在前台模式下工作
这是次优的,因为我们没有得到进入和退出事件或状态,但至少我们得到了信标计数。