didEnterRegion有时只会被解雇

时间:2017-07-28 12:48:29

标签: swift callback ibeacon

我编写了下面的代码,这是我能想到的最基本的Swift 3代码,用于检查我的应用是否可以检测到iBeacon是否可用。 然而,didEnterRegion只在蓝色月亮中发射一次。我可能在我的手机上跑了20次,而且它只被射了两次。这个问题似乎不是灯塔本身,因为当我使用App商店的应用程序时,它总能检测到我的iBeacon。有什么想法为什么很少调用这个回调? PS:我已经在Info.plist中添加了隐私 - 位置始终使用说明

import UIKit
import CoreLocation

class ViewController: UIViewController {

let locationManager = CLLocationManager()

@IBOutlet weak var textLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    locationManager.requestAlwaysAuthorization()
    locationManager.delegate = self
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func startMonitoring(_ sender: Any) {
    if let uuid = UUID(uuidString: "10F86430-1346-11E4-9191-0800200C9A66") {
        let beaconRegion = CLBeaconRegion(proximityUUID: uuid, major: 1, minor: 1,identifier: "iBeacon")
        locationManager.startMonitoring(for: beaconRegion)
        textLabel.text = "Monitoring"
    }
}
}

extension ViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, monitoringDidFailFor region: CLRegion?, withError error: Error) {
    textLabel.text = "\(error.localizedDescription)"
}

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
    textLabel.text = "\(error.localizedDescription)"
}

func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
        textLabel.text = "Found a new beacon"
}
}

1 个答案:

答案 0 :(得分:1)

您必须退出该区域,等待30秒左右,然后再次输入以触发该区域。如果你住在该地区,它将不会触发。