gpt4 book ai didi

swift - didEnterRegion 有时只会被解雇

转载 作者:行者123 更新时间:2023-11-28 15:25:09 25 4
gpt4 key购买 nike

我写了下面的代码,这是我能想到的最基本的 Swift 3 代码,用于检查我的应用程序是否可以检测 iBeacon 是否可用。然而,didEnterRegion 只在极少数情况下启动一次。我大概在我的手机上运行了 20 次,它只被触发了两次。问题似乎不是信标本身,因为当我使用 App 商店中的应用程序时,它总是检测到我的 iBeacon。知道为什么很少调用此回调吗?PS:我已经在Info.plist中添加了Privacy - Location Always Usage Description

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"
}
}

最佳答案

您必须退出该区域,等待 30 秒左右,然后再次进入才能触发。如果您住在该地区,则不会触发。

关于swift - didEnterRegion 有时只会被解雇,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45373977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com