用于信标检测的Swift2.3代码 [英] Swift2.3 code for Beacon detection

查看:121
本文介绍了用于信标检测的Swift2.3代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We are in advanced stages of developing a Swift2.2 app and hence have decided to migrate to 2.3 in the interim and do the full Swift 3 migration later. However we are unable to get beacon detection working post conversion to Swift 2.3. The method "didRangeBeacons" keeps returning an empty array. The same code was working in Swift 2.2 so we know we have all the permissions etc in place.

此外,如果我们在同一ipad上打开"Locate"应用程序,则我们的应用程序还将开始返回"didRangeBeacons"中的数据.已经尝试了各种版本的应用程序,所有Swift2.3应用程序的行为都相同.无法确定定位"应用程序在做什么...任何人都在同一条船上?

Also if we open the "Locate" app on the same ipad then our app also starts returning data in "didRangeBeacons". Have tried various versions of the apps out there and all Swift2.3 apps are behaving the same way. Can't make out what the Locate app is doing... Anyone on the same boat??

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

Here is the code we are using. Am not sure this is supposed to be written here or in comments but couldn't put code within comments somehow...

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()之后

[Update post some more attempts to get this working] App works in foreground mode if we remove self.locationManager.startMonitoringForRegion(self.region) and call self.locationManager.startRangingBeaconsInRegion(self.region) directly after self.locationManager.requestAlwaysAuthorization()

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

This is sub-optimal because we don't get entry and exit events or state but atleast we are getting beacon counts.

推荐答案

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

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

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

This is presumably a bug that will be fixed in an iOS update. Until then, some users have reported that setting the app deployment target in XCode to 9.x will resolve the issue.

这篇关于用于信标检测的Swift2.3代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆