Ios 多次调用检测信标在范围内/范围外的时间
我有一个viewController,它可以检测信标是否在范围内或超出范围,并发送本地通知,并在数据库中保存一些数据Ios 多次调用检测信标在范围内/范围外的时间,ios,swift,ibeacon,Ios,Swift,Ibeacon,我有一个viewController,它可以检测信标是否在范围内或超出范围,并发送本地通知,并在数据库中保存一些数据 class FirstViewController: UIViewController, CLLocationManagerDelegate { var locationManager: CLLocationManager! . . . override func viewDidLoad() { super.viewDidLoad(
class FirstViewController: UIViewController, CLLocationManagerDelegate {
var locationManager: CLLocationManager!
.
.
.
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
locationManager.requestAlwaysAuthorization()
}
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .AuthorizedWhenInUse || status == .AuthorizedAlways{
if CLLocationManager.isMonitoringAvailableForClass(CLBeaconRegion.self) {
if CLLocationManager.isRangingAvailable() {
startScanning()
}
}
}
}
func startScanning() {
for var i=0; i<deviceObjectArray.count; i++ {
let uuid = NSUUID(UUIDString: deviceObjectArray[i].uuid)!
let major = UInt16(deviceObjectArray[i].major)
let minor = UInt16(deviceObjectArray[i].minor)
beaconRegionsArray.append(CLBeaconRegion(proximityUUID: uuid, major: major!, minor: minor!, identifier: deviceObjectArray[i].deviceName))
}
for var i=0; i<beaconRegionsArray.count; i++ {
locationManager.startMonitoringForRegion(beaconRegionsArray[i])
}
}
func locationManager(manager: CLLocationManager, didEnterRegion region: CLRegion) {
print("Entering region")
myTitle = "Entering the range"
var alertView = UIAlertController(title: myTitle, message: "Press OK to continue", preferredStyle: UIAlertControllerStyle.Alert)
alertView.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: yesHandler))
presentViewController(alertView, animated: true, completion: nil)
alertBody1 = "Entering beacon range"
LocalNotification()
}
func locationManager(manager: CLLocationManager, didExitRegion region: CLRegion) {
print("Exiting region")
myTitle = "Exiting the range"
var alertView = UIAlertController(title: myTitle, message: "Press OK to continue", preferredStyle: UIAlertControllerStyle.Alert)
alertView.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: yesHandler))
presentViewController(alertView, animated: true, completion: nil)
alertBody1 = "Exiting beacon range"
LocalNotification()
setMyLocation(region.identifier)
}
我接到几次电话,突然收到10或15条信息
“退出信标范围”
同样的数据也会保存10到15次。
我如何解决这个问题
我应该自己创建一个类并将其与ViewController隔离吗?
不知道我是否足够清楚,但在此方面的任何帮助都是非常感谢的 不清楚为什么会出现额外的退出事件,但可能是因为您正在尝试注册超过20个允许的监视区域。当超过限制时,旧区域由CoreLocation卸载时,可能会出现退出事件 超出限制的问题是因为在加载ViewController时调用了
startMonitoringForRegion
,但在ViewController变为非活动状态时,从未对每个区域调用stopMonitoringFirRegion
我会改变这个,也许它会解决你的问题。只是一个猜测——如果这不起作用,可能是时候按照您的建议将beacon代码与ViewController解耦了。谢谢您提供的信息。在我的应用程序中,我将使用4或5个视图控制器,可以在它们之间来回移动。如果我在注册的信标范围内,无论我当前在哪个viewcontroller中,我也希望能够获得通知。设计这样一个应用程序的最佳方式是什么?我的所有viewcontroller类都应该包含CLLocationManagerDelegate吗?应用程序通常会将信标检测代码集中在AppDelegate中。
func locationManager(manager: CLLocationManager, didExitRegion region: CLRegion) {