Xcode Swift iBeacon 从 AppDelegate

Xcode Swift iBeacon ranging from AppDelegate

我正在 AppDelegate 中执行 startRangingBeaconsInRegion 一旦检测到信标...主视图控制器 -> 转到 -> ABC ViewController 我想停止测距,直到用户单击 'Back' 按钮。 我怎样才能停止新 ViewController ABC 的测距?当按下 'Back' 按钮时,重新开始测距? 谢谢!

如果您只想在 ABCViewController 处于活动状态时停止信标测距,您可以在 AppDelegate 中添加两种方法,如下所示:

func stopRanging() {
  locationManager.stopRangingBeaconsInRegion(region)
}

func startRanging() {
  locationManager.startRangingBeaconsInRegion(region)
}

然后在你的 ABCViewController 中你可以添加这样的方法,这将导致在视图控制器显示时停止测距,并在它不可见时重新启动:

override func viewWillAppear(animated: Bool) {
  super.viewWillAppear(animated)
  let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
  appDelegate.stopRanging()
}

override func viewWillDisappear(animated: Bool) {
  super.viewWillDisappear(animated)
  let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
  appDelegate.startRanging()
}